Upload files to "renderer"
This commit is contained in:
@@ -0,0 +1,247 @@
|
||||
// Media place-marker code: pictures, thumbnails, video, sound. Nothing is uploaded.
|
||||
// buildCode() is pure and also runs under Node for tests.
|
||||
(function (root) {
|
||||
'use strict';
|
||||
|
||||
const KINDS = [
|
||||
{ id: 'picture', label: 'Picture' },
|
||||
{ id: 'thumb', label: 'Thumbnail' },
|
||||
{ id: 'video', label: 'Video file' },
|
||||
{ id: 'vembed', label: 'Video embed' },
|
||||
{ id: 'sound', label: 'Sound file' },
|
||||
{ id: 'sembed', label: 'Sound embed' }
|
||||
];
|
||||
|
||||
const DEFAULTS = {
|
||||
picture: { file: 'Example.jpg', align: 'none', width: '300', alt: '' },
|
||||
thumb: { file: 'Example.jpg', type: 'thumb', align: 'right', width: '250', caption: 'Caption' },
|
||||
video: { file: 'Example.webm', type: 'thumb', align: 'right', width: '320', caption: 'Caption', start: '', end: '' },
|
||||
vembed: { service: 'url', url: 'https://www.youtube.com/watch?v=VIDEOID', id: 'VIDEOID', align: 'right', width: '560', height: '315', caption: 'Caption' },
|
||||
sound: { file: 'Example.ogg', mode: 'player', align: 'right', width: '', caption: 'Caption' },
|
||||
sembed: { service: 'url', url: 'https://example.com/sound.mp3', id: '', align: 'right', caption: 'Caption' }
|
||||
};
|
||||
|
||||
const clean = (s) => String(s === undefined || s === null ? '' : s).replace(/^\s*(File|Image|Media):/i, '').trim();
|
||||
const px = (w) => (String(w || '').trim() ? String(w).trim().replace(/px$/i, '') + 'px' : '');
|
||||
const cap = (s) => String(s || '').replace(/\|/g, '{{!}}').trim();
|
||||
|
||||
function buildCode(kind, o) {
|
||||
o = Object.assign({}, DEFAULTS[kind], o || {});
|
||||
const parts = [];
|
||||
if (kind === 'picture') {
|
||||
parts.push('File:' + clean(o.file));
|
||||
if (o.align && o.align !== 'none') parts.push(o.align);
|
||||
if (px(o.width)) parts.push(px(o.width));
|
||||
if (o.alt) parts.push('alt=' + cap(o.alt));
|
||||
return '[[' + parts.join('|') + ']]';
|
||||
}
|
||||
if (kind === 'thumb' || kind === 'video') {
|
||||
parts.push('File:' + clean(o.file));
|
||||
if (o.type && o.type !== 'none') parts.push(o.type);
|
||||
if (o.align && o.align !== 'none') parts.push(o.align);
|
||||
if (o.type !== 'frame' && px(o.width)) parts.push(px(o.width));
|
||||
if (kind === 'video') {
|
||||
if (String(o.start || '').trim()) parts.push('start=' + String(o.start).trim());
|
||||
if (String(o.end || '').trim()) parts.push('end=' + String(o.end).trim());
|
||||
}
|
||||
if (cap(o.caption)) parts.push(cap(o.caption));
|
||||
return '[[' + parts.join('|') + ']]';
|
||||
}
|
||||
if (kind === 'sound') {
|
||||
if (o.mode === 'link') return '[[Media:' + clean(o.file) + '|' + (cap(o.caption) || 'Listen') + ']]';
|
||||
parts.push('File:' + clean(o.file));
|
||||
parts.push('thumb');
|
||||
if (o.align && o.align !== 'none') parts.push(o.align);
|
||||
if (px(o.width)) parts.push(px(o.width));
|
||||
if (cap(o.caption)) parts.push(cap(o.caption));
|
||||
return '[[' + parts.join('|') + ']]';
|
||||
}
|
||||
if (kind === 'vembed' || kind === 'sembed') {
|
||||
if (o.service && o.service !== 'url') {
|
||||
const a = ['{{#ev:' + o.service, String(o.id || '').trim()];
|
||||
const extra = kind === 'vembed'
|
||||
? [String(o.width || '').trim(), o.align && o.align !== 'none' ? o.align : '', cap(o.caption)]
|
||||
: ['', o.align && o.align !== 'none' ? o.align : '', cap(o.caption)];
|
||||
while (extra.length && !extra[extra.length - 1]) extra.pop();
|
||||
return a.concat(extra).join('|') + '}}';
|
||||
}
|
||||
const out = ['{{#evu:' + String(o.url || '').trim()];
|
||||
if (o.align && o.align !== 'none') out.push('alignment=' + o.align);
|
||||
if (kind === 'vembed') {
|
||||
const w = String(o.width || '').trim();
|
||||
const h = String(o.height || '').trim();
|
||||
if (w && h) out.push('dimensions=' + w + 'x' + h);
|
||||
else if (w) out.push('dimensions=' + w);
|
||||
}
|
||||
if (cap(o.caption)) { out.push('description=' + cap(o.caption)); out.push('container=frame'); }
|
||||
return out.join('|') + '}}';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
const pure = { buildCode, KINDS, DEFAULTS };
|
||||
if (typeof module !== 'undefined' && module.exports) module.exports = pure;
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
/* ---------- dialog ---------- */
|
||||
|
||||
const NOTES = {
|
||||
picture: 'Place-marker only. Nothing is uploaded: use the name of a file that is, or will be, on your wiki.',
|
||||
thumb: 'Place-marker only. Nothing is uploaded. A thumbnail gets a border, the caption underneath and floats to the side you pick.',
|
||||
video: 'Place-marker only. Playing video files in a page needs the TimedMediaHandler extension on your wiki (or video files allowed by your wiki\'s settings).',
|
||||
vembed: 'Video embeds need an embed extension such as EmbedVideo on your wiki. Without it the code shows as plain text. Check which one your wiki uses.',
|
||||
sound: 'Place-marker only. Nothing is uploaded. Playing sound files in a page needs the TimedMediaHandler extension on your wiki.',
|
||||
sembed: 'Sound embeds need an embed extension such as EmbedVideo on your wiki. Without it the code shows as plain text.'
|
||||
};
|
||||
|
||||
const WARN = {
|
||||
video: 'Video files need the TimedMediaHandler extension on your wiki to play.',
|
||||
vembed: 'Embeds need the EmbedVideo extension (or your wiki\'s equivalent).',
|
||||
sound: 'Sound files need the TimedMediaHandler extension on your wiki to play.',
|
||||
sembed: 'Embeds need the EmbedVideo extension (or your wiki\'s equivalent).'
|
||||
};
|
||||
|
||||
function init(opts) {
|
||||
const api = opts.api;
|
||||
const toast = opts.toast;
|
||||
const openModal = opts.openModal;
|
||||
const btn = document.getElementById('btnMedia');
|
||||
if (!btn) return;
|
||||
btn.addEventListener('mousedown', (e) => e.preventDefault());
|
||||
btn.addEventListener('click', () => open('picture'));
|
||||
|
||||
function open(start) {
|
||||
let kind = start;
|
||||
const vals = {};
|
||||
Object.keys(DEFAULTS).forEach((k) => { vals[k] = Object.assign({}, DEFAULTS[k]); });
|
||||
|
||||
const wrap = document.createElement('div');
|
||||
const tabs = document.createElement('div');
|
||||
tabs.className = 'mTabs';
|
||||
const form = document.createElement('div');
|
||||
const note = document.createElement('p');
|
||||
note.className = 'mNote';
|
||||
const codeLabel = document.createElement('div');
|
||||
codeLabel.className = 'mCodeLabel';
|
||||
codeLabel.textContent = 'Code that will be inserted';
|
||||
const code = document.createElement('textarea');
|
||||
code.className = 'mCode';
|
||||
code.readOnly = true;
|
||||
code.rows = 3;
|
||||
code.spellcheck = false;
|
||||
const copy = document.createElement('button');
|
||||
copy.textContent = 'Copy code';
|
||||
copy.className = 'mCopy';
|
||||
wrap.appendChild(tabs);
|
||||
wrap.appendChild(note);
|
||||
wrap.appendChild(form);
|
||||
wrap.appendChild(codeLabel);
|
||||
wrap.appendChild(code);
|
||||
wrap.appendChild(copy);
|
||||
|
||||
const sel = (o, list) => {
|
||||
const s = document.createElement('select');
|
||||
list.forEach((p) => { const x = document.createElement('option'); x.value = p[0]; x.textContent = p[1]; s.appendChild(x); });
|
||||
s.value = o;
|
||||
return s;
|
||||
};
|
||||
const ALIGN = [['none', 'No alignment'], ['left', 'Left'], ['center', 'Center'], ['right', 'Right']];
|
||||
|
||||
function field(label, key, type, list, hint) {
|
||||
const row = document.createElement('div');
|
||||
row.className = 'formRow';
|
||||
const l = document.createElement('label');
|
||||
l.textContent = label;
|
||||
row.appendChild(l);
|
||||
let input;
|
||||
if (type === 'select') input = sel(vals[kind][key], list);
|
||||
else { input = document.createElement('input'); input.value = vals[kind][key]; input.spellcheck = false; if (hint) input.placeholder = hint; }
|
||||
const upd = () => { vals[kind][key] = input.value; refresh(); };
|
||||
input.addEventListener('input', upd);
|
||||
input.addEventListener('change', () => { upd(); if (type === 'select') draw(); });
|
||||
row.appendChild(input);
|
||||
form.appendChild(row);
|
||||
}
|
||||
|
||||
function draw() {
|
||||
tabs.innerHTML = '';
|
||||
KINDS.forEach((k) => {
|
||||
const b = document.createElement('button');
|
||||
b.textContent = k.label;
|
||||
b.classList.toggle('on', k.id === kind);
|
||||
b.addEventListener('click', () => { kind = k.id; draw(); });
|
||||
tabs.appendChild(b);
|
||||
});
|
||||
note.textContent = NOTES[kind];
|
||||
form.innerHTML = '';
|
||||
const o = vals[kind];
|
||||
if (kind === 'picture') {
|
||||
field('File name', 'file', 'text', null, 'Example.jpg');
|
||||
field('Alignment', 'align', 'select', ALIGN);
|
||||
field('Width (px)', 'width', 'text', null, 'blank for full size');
|
||||
field('Alt text', 'alt', 'text');
|
||||
} else if (kind === 'thumb' || kind === 'video') {
|
||||
field('File name', 'file', 'text', null, kind === 'video' ? 'Example.webm' : 'Example.jpg');
|
||||
field('Style', 'type', 'select', [['thumb', 'Thumbnail (border and caption)'], ['frame', 'Frame (full size, caption)'], ['frameless', 'Frameless (no border)'], ['none', 'Plain']]);
|
||||
field('Alignment', 'align', 'select', ALIGN);
|
||||
if (o.type !== 'frame') field('Width (px)', 'width', 'text', null, 'e.g. 250');
|
||||
if (kind === 'video') {
|
||||
field('Start (sec)', 'start', 'text', null, 'optional');
|
||||
field('End (sec)', 'end', 'text', null, 'optional');
|
||||
}
|
||||
field('Caption', 'caption', 'text');
|
||||
} else if (kind === 'sound') {
|
||||
field('File name', 'file', 'text', null, 'Example.ogg');
|
||||
field('Form', 'mode', 'select', [['player', 'Player box'], ['link', 'Plain link to the file']]);
|
||||
if (o.mode !== 'link') {
|
||||
field('Alignment', 'align', 'select', ALIGN);
|
||||
field('Width (px)', 'width', 'text', null, 'optional');
|
||||
}
|
||||
field('Caption', 'caption', 'text');
|
||||
} else if (kind === 'vembed') {
|
||||
field('Source', 'service', 'select', [['url', 'Full URL'], ['youtube', 'YouTube id'], ['vimeo', 'Vimeo id'], ['dailymotion', 'Dailymotion id']]);
|
||||
if (o.service === 'url') field('Video URL', 'url', 'text'); else field('Video id', 'id', 'text');
|
||||
field('Alignment', 'align', 'select', ALIGN);
|
||||
field('Width (px)', 'width', 'text');
|
||||
if (o.service === 'url') field('Height (px)', 'height', 'text');
|
||||
field('Caption', 'caption', 'text');
|
||||
} else if (kind === 'sembed') {
|
||||
field('Source', 'service', 'select', [['url', 'Full URL'], ['soundcloud', 'SoundCloud id'], ['bandcamp', 'Bandcamp id']]);
|
||||
if (o.service === 'url') field('Sound URL', 'url', 'text'); else field('Sound id', 'id', 'text');
|
||||
field('Alignment', 'align', 'select', ALIGN);
|
||||
field('Caption', 'caption', 'text');
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
||||
function refresh() { code.value = buildCode(kind, vals[kind]); }
|
||||
|
||||
copy.addEventListener('click', async () => {
|
||||
code.select();
|
||||
let ok = false;
|
||||
try { ok = document.execCommand('copy'); } catch (e) { ok = false; }
|
||||
if (!ok) { try { await navigator.clipboard.writeText(code.value); ok = true; } catch (e) { ok = false; } }
|
||||
toast(ok ? 'Code copied.' : 'Could not copy. Select the code and press Ctrl+C.');
|
||||
});
|
||||
|
||||
draw();
|
||||
const m = openModal({
|
||||
title: 'Media place-marker code',
|
||||
content: wrap,
|
||||
wide: true,
|
||||
noFocus: true,
|
||||
buttons: [{ label: 'Cancel', value: null }, { label: 'Insert', value: 'ok', primary: true }]
|
||||
});
|
||||
m.promise.then((v) => {
|
||||
if (v !== 'ok') return;
|
||||
const text = buildCode(kind, vals[kind]);
|
||||
api.insert(text);
|
||||
if (WARN[kind]) toast(WARN[kind], 7000);
|
||||
});
|
||||
}
|
||||
|
||||
return { open };
|
||||
}
|
||||
|
||||
window.SwiftMedia = Object.assign({ init }, pure);
|
||||
})(this);
|
||||
Reference in New Issue
Block a user