return resp.data.content;
}
-export default {show, close, upload, load};
\ No newline at end of file
+
+function buildDrawingContentHtml(drawing) {
+ const isSvg = drawing.url.split('.').pop().toLowerCase() === 'svg';
+ const image = `<img src="${drawing.url}">`;
+ const embed = `<embed src="${drawing.url}" type="image/svg+xml">`;
+ return `<div drawio-diagram="${drawing.id}">${isSvg ? embed : image}</div>`
+}
+
+function buildDrawingContentNode(drawing) {
+ const div = document.createElement('div');
+ div.innerHTML = buildDrawingContentHtml(drawing);
+ return div.children[0];
+}
+
+export default {show, close, upload, load, buildDrawingContentHtml, buildDrawingContentNode};
\ No newline at end of file
import DrawIO from "../services/drawio";
+import {build} from "./config";
let pageEditor = null;
let currentNode = null;
function showDrawingManager(mceEditor, selectedNode = null) {
pageEditor = mceEditor;
currentNode = selectedNode;
+
// Show image manager
window.ImageManager.show(function (image) {
if (selectedNode) {
- let imgElem = selectedNode.querySelector('img');
- pageEditor.dom.setAttrib(imgElem, 'src', image.url);
- pageEditor.dom.setAttrib(selectedNode, 'drawio-diagram', image.id);
+ pageEditor.dom.replace(buildDrawingNode(image), selectedNode);
} else {
- let imgHTML = `<div drawio-diagram="${image.id}" contenteditable="false"><img src="${image.url}"></div>`;
- pageEditor.insertContent(imgHTML);
+ const drawingHtml = DrawIO.buildDrawingContentHtml(image);
+ pageEditor.insertContent(drawingHtml);
}
}, 'drawio');
}
DrawIO.show(options.drawioUrl, drawingInit, updateContent);
}
+function buildDrawingNode(drawing) {
+ const drawingEl = DrawIO.buildDrawingContentNode(drawing);
+ drawingEl.setAttribute('contenteditable', 'false');
+ drawingEl.setAttribute('data-ephox-embed-iri', 'true');
+ return drawingEl;
+}
+
async function updateContent(drawingData) {
const id = "image-" + Math.random().toString(16).slice(2);
const loadingImage = window.baseUrl('/loading.gif');
// Handle updating an existing image
if (currentNode) {
DrawIO.close();
- let imgElem = currentNode.querySelector('img');
try {
const img = await DrawIO.upload(drawingData, options.pageId);
- pageEditor.dom.setAttrib(imgElem, 'src', img.url);
- pageEditor.dom.setAttrib(currentNode, 'drawio-diagram', img.id);
+ pageEditor.dom.replace(buildDrawingNode(img), currentNode);
} catch (err) {
handleUploadError(err);
}
}
setTimeout(async () => {
- pageEditor.insertContent(`<div drawio-diagram contenteditable="false"><img src="${loadingImage}" id="${id}"></div>`);
+ pageEditor.insertContent(`<div drawio-diagram contenteditable="false"><img src="${loadingImage}" alt="Loading" id="${id}"></div>`);
DrawIO.close();
try {
const img = await DrawIO.upload(drawingData, options.pageId);
- pageEditor.dom.setAttrib(id, 'src', img.url);
- pageEditor.dom.get(id).parentNode.setAttribute('drawio-diagram', img.id);
+ pageEditor.dom.replace(buildDrawingNode(img), pageEditor.dom.get(id).parentNode);
} catch (err) {
pageEditor.dom.remove(id);
handleUploadError(err);
}
/**
- *
* @param {WysiwygConfigOptions} providedOptions
* @return {function(Editor, string)}
*/
showDrawingEditor(editor, selectedNode);
});
- editor.on('SetContent', function () {
- const drawings = editor.$('body > div[drawio-diagram]');
- if (!drawings.length) return;
+ editor.on('PreInit', () => {
+ editor.parser.addNodeFilter('div', function(nodes) {
+ for (const node of nodes) {
+ if (node.attr('drawio-diagram')) {
+ // Set content editable to be false to prevent direct editing of child content.
+ node.attr('contenteditable', 'false');
+ // Set this attribute to prevent drawing contents being parsed as media embeds
+ // to avoid contents being replaced with placeholder images.
+ // TinyMCE embed plugin sources looks for this attribute in its logic.
+ node.attr('data-ephox-embed-iri', 'true');
+ }
+ }
+ });
- editor.undoManager.transact(function () {
- drawings.each((index, elem) => {
- elem.setAttribute('contenteditable', 'false');
- });
+ editor.serializer.addNodeFilter('div', function(nodes) {
+ for (const node of nodes) {
+ // Clean up content attributes
+ if (node.attr('drawio-diagram')) {
+ node.attr('contenteditable', null);
+ node.attr('data-ephox-embed-iri', null);
+ }
+ }
});
});