mirror of
https://github.com/itflow-org/itflow
synced 2026-12-02 00:59:03 +00:00
140 lines
6.4 KiB
JavaScript
140 lines
6.4 KiB
JavaScript
/*
|
|
* TinyMCE's defaults, set once for every page - loaded straight after
|
|
* tinymce.min.js, before any editor is set up.
|
|
*
|
|
* TinyMCE fetches the rest of itself when an editor opens - its plugins, theme,
|
|
* icons, model and skin stylesheets - so those never pass through a <script> or
|
|
* <link> tag of ours, and staticFileUrl() can't version them. cache_suffix puts
|
|
* a version on everything it fetches: the same ?v= as tinymce.min.js's own tag,
|
|
* which changes when an update brings a new TinyMCE. Without it, browsers ran
|
|
* the new editor with the old copies of its parts still in their cache.
|
|
*/
|
|
(function () {
|
|
if (!window.tinymce) {
|
|
return;
|
|
}
|
|
|
|
var tag = document.querySelector('script[src*="/libs/tinymce/tinymce.min.js"]');
|
|
var version = tag ? new URL(tag.src, document.baseURI).searchParams.get('v') : null;
|
|
|
|
if (version) {
|
|
tinymce.overrideDefaults({ cache_suffix: '?v=' + version });
|
|
}
|
|
})();
|
|
|
|
/*
|
|
* A picture copied by itself - Copy image in a browser, a picture copied out of
|
|
* Word or Outlook - puts two things on the clipboard: the picture, and a scrap
|
|
* of HTML that is only an <img> pointing at where it came from. TinyMCE takes
|
|
* the HTML when there is any, so the editor got a link to the other site: the
|
|
* person pasting saw the picture, signed in over there as they are, and nobody
|
|
* else did. When the clipboard holds one picture and nothing else, the paste
|
|
* is handed back to TinyMCE with only the picture on it, and it is kept like a
|
|
* pasted screenshot. Text copied with pictures in it is left alone - the
|
|
* browser doesn't put those pictures on the clipboard, only where they are.
|
|
*/
|
|
(function () {
|
|
if (!window.tinymce || typeof DataTransfer !== 'function' || typeof DOMParser !== 'function') {
|
|
return;
|
|
}
|
|
|
|
// The picture, when it is all that was copied - null for anything else
|
|
function pictureCopiedAlone(clipboard) {
|
|
if (!clipboard || !clipboard.items || Array.prototype.indexOf.call(clipboard.types || [], 'x-tinymce/html') !== -1) {
|
|
return null;
|
|
}
|
|
|
|
var pictures = [];
|
|
for (var i = 0; i < clipboard.items.length; i++) {
|
|
var item = clipboard.items[i];
|
|
if (item.kind === 'file' && /^image\/(png|jpeg|gif|webp|bmp)$/.test(item.type)) {
|
|
pictures.push(item.getAsFile());
|
|
}
|
|
}
|
|
if (pictures.length !== 1 || !pictures[0]) {
|
|
return null;
|
|
}
|
|
|
|
// With no HTML beside the picture TinyMCE keeps it already
|
|
var html = clipboard.getData('text/html');
|
|
if (!html) {
|
|
return null;
|
|
}
|
|
|
|
// Only an <img> that points somewhere, with no words or anything else around it
|
|
var body = new DOMParser().parseFromString(html, 'text/html').body;
|
|
var images = body.querySelectorAll('img');
|
|
var alone = images.length === 1
|
|
&& body.textContent.trim() === ''
|
|
&& !body.querySelector('table, video, audio, iframe, object, embed, svg, canvas, hr, input')
|
|
&& !/^data:/i.test(images[0].getAttribute('src') || '');
|
|
return alone ? pictures[0] : null;
|
|
}
|
|
|
|
tinymce.on('AddEditor', function (added) {
|
|
var editor = added.editor;
|
|
|
|
// Its form posts as multipart, so what is written can go as a file (itflowFields, js/http.js)
|
|
var element = editor.getElement();
|
|
if (window.itflowFields && element && element.form) {
|
|
window.itflowFields.multipart(element.form);
|
|
}
|
|
|
|
// Ahead of TinyMCE's own paste handling, which leaves a paste that was already dealt with alone
|
|
editor.on('paste', function (event) {
|
|
if (event.isDefaultPrevented() || !editor.options.get('paste_data_images')) {
|
|
return;
|
|
}
|
|
var picture = pictureCopiedAlone(event.clipboardData);
|
|
if (!picture) {
|
|
return;
|
|
}
|
|
var only = new DataTransfer();
|
|
only.items.add(picture);
|
|
event.preventDefault();
|
|
editor.dispatch('paste', { clipboardData: only });
|
|
}, true);
|
|
});
|
|
})();
|
|
|
|
/**
|
|
* A document as it reads on its page (.itflow-prose, css/itflow_custom.css), for the editing
|
|
* surface - which is an iframe, so the page's stylesheet and custom properties don't reach it
|
|
* and the values are passed in as literal text, as itflowTinyMceSkin() does. A document's editor
|
|
* uses it for agents (js/app.js) and in the client portal (js/portal.js), so it is here.
|
|
*/
|
|
function itflowProseContentStyle() {
|
|
var root = getComputedStyle(document.documentElement);
|
|
|
|
function v(name, fallback) {
|
|
var value = root.getPropertyValue(name).trim();
|
|
return value || fallback;
|
|
}
|
|
|
|
var border = v('--bs-border-color', '#dee2e6');
|
|
var tint = v('--bs-tertiary-bg', '#f8f9fa');
|
|
var muted = v('--bs-secondary-color', '#6c757d');
|
|
var strong = v('--bs-emphasis-color', '#000');
|
|
|
|
return 'body{line-height:1.65;margin:1.25rem 1.5rem;}' +
|
|
'body>:first-child{margin-top:0;}' +
|
|
'h1,h2,h3,h4,h5,h6{margin:1.75rem 0 .6rem;font-weight:600;line-height:1.3;color:' + strong + ';}' +
|
|
'h1{font-size:1.6rem;}' +
|
|
'h2{font-size:1.3rem;padding-bottom:.35rem;border-bottom:1px solid ' + border + ';}' +
|
|
'h3{font-size:1.12rem;}' +
|
|
'h4{font-size:1rem;}' +
|
|
'h5,h6{font-size:.9rem;text-transform:uppercase;letter-spacing:.03em;color:' + muted + ';}' +
|
|
'p,ul,ol,table,pre,blockquote,figure{margin:0 0 1rem;}' +
|
|
'ul,ol{padding-left:1.5rem;}' +
|
|
'li+li{margin-top:.25rem;}' +
|
|
'hr{margin:2rem 0;border:0;border-top:1px solid ' + border + ';}' +
|
|
'img{max-width:100%;height:auto;border-radius:.375rem;}' +
|
|
'table{border-collapse:collapse;font-size:.925rem;}' +
|
|
// Every table with its lines, as on the page - not TinyMCE's dashed outline for one without a border
|
|
'table td,table th{padding:.4rem .65rem;border:1px solid ' + border + ' !important;}' +
|
|
'table th,table thead td{background-color:' + tint + ';font-weight:600;}' +
|
|
'pre{padding:.75rem 1rem;border:1px solid ' + border + ';border-radius:.375rem;background-color:' + tint + ';white-space:pre;overflow-x:auto;}' +
|
|
':not(pre)>code{padding:.1rem .3rem;border-radius:.25rem;background-color:' + tint + ';color:' + strong + ';}' +
|
|
'blockquote{margin-left:0;padding:.25rem 0 .25rem 1rem;border-left:.25rem solid ' + border + ';color:' + muted + ';}';
|
|
}
|