mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-27 06:01:54 +03:00
Drawio: Started browser drawing backup store system
Adds just the part to store image data, and remove on successfull save. Alters save events to properly throw upon error. Adds IDB-Keyval library for local large-size store. For #4421
This commit is contained in:
@ -82,18 +82,20 @@ export class Actions {
|
||||
|
||||
const selectionRange = this.#getSelectionRange();
|
||||
|
||||
DrawIO.show(url, () => Promise.resolve(''), pngData => {
|
||||
DrawIO.show(url, () => Promise.resolve(''), async pngData => {
|
||||
const data = {
|
||||
image: pngData,
|
||||
uploaded_to: Number(this.editor.config.pageId),
|
||||
};
|
||||
|
||||
window.$http.post('/images/drawio', data).then(resp => {
|
||||
try {
|
||||
const resp = await window.$http.post('/images/drawio', data);
|
||||
this.#insertDrawing(resp.data, selectionRange);
|
||||
DrawIO.close();
|
||||
}).catch(err => {
|
||||
} catch (err) {
|
||||
this.handleDrawingUploadError(err);
|
||||
});
|
||||
throw new Error(`Failed to save image with error: ${err}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -112,13 +114,14 @@ export class Actions {
|
||||
const selectionRange = this.#getSelectionRange();
|
||||
const drawingId = imgContainer.getAttribute('drawio-diagram');
|
||||
|
||||
DrawIO.show(drawioUrl, () => DrawIO.load(drawingId), pngData => {
|
||||
DrawIO.show(drawioUrl, () => DrawIO.load(drawingId), async pngData => {
|
||||
const data = {
|
||||
image: pngData,
|
||||
uploaded_to: Number(this.editor.config.pageId),
|
||||
};
|
||||
|
||||
window.$http.post('/images/drawio', data).then(resp => {
|
||||
try {
|
||||
const resp = await window.$http.post('/images/drawio', data);
|
||||
const newText = `<div drawio-diagram="${resp.data.id}"><img src="${resp.data.url}"></div>`;
|
||||
const newContent = this.#getText().split('\n').map(line => {
|
||||
if (line.indexOf(`drawio-diagram="${drawingId}"`) !== -1) {
|
||||
@ -128,9 +131,10 @@ export class Actions {
|
||||
}).join('\n');
|
||||
this.#setText(newContent, selectionRange);
|
||||
DrawIO.close();
|
||||
}).catch(err => {
|
||||
} catch (err) {
|
||||
this.handleDrawingUploadError(err);
|
||||
});
|
||||
throw new Error(`Failed to save image with error: ${err}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1,17 +1,22 @@
|
||||
// Docs: https://www.diagrams.net/doc/faq/embed-mode
|
||||
import * as store from './store';
|
||||
|
||||
let iFrame = null;
|
||||
let lastApprovedOrigin;
|
||||
let onInit; let
|
||||
onSave;
|
||||
let onInit;
|
||||
let onSave;
|
||||
const saveBackupKey = 'last-drawing-save';
|
||||
|
||||
function drawPostMessage(data) {
|
||||
iFrame.contentWindow.postMessage(JSON.stringify(data), lastApprovedOrigin);
|
||||
}
|
||||
|
||||
function drawEventExport(message) {
|
||||
store.set(saveBackupKey, message.data);
|
||||
if (onSave) {
|
||||
onSave(message.data);
|
||||
onSave(message.data).then(() => {
|
||||
store.del(saveBackupKey);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,9 +69,11 @@ function drawReceive(event) {
|
||||
|
||||
/**
|
||||
* Show the draw.io editor.
|
||||
* onSaveCallback must return a promise that resolves on successful save and errors on failure.
|
||||
* onInitCallback must return a promise with the xml to load for the editor.
|
||||
* @param {String} drawioUrl
|
||||
* @param {Function} onInitCallback - Must return a promise with the xml to load for the editor.
|
||||
* @param {Function} onSaveCallback - Is called with the drawing data on save.
|
||||
* @param {Function<Promise<String>>} onInitCallback
|
||||
* @param {Function<Promise>} onSaveCallback - Is called with the drawing data on save.
|
||||
*/
|
||||
export function show(drawioUrl, onInitCallback, onSaveCallback) {
|
||||
onInit = onInitCallback;
|
||||
|
1
resources/js/services/store.js
Normal file
1
resources/js/services/store.js
Normal file
@ -0,0 +1 @@
|
||||
export { get, set, del } from 'idb-keyval';
|
@ -70,3 +70,14 @@ export function uniqueId() {
|
||||
const S4 = () => (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
|
||||
return (`${S4() + S4()}-${S4()}-${S4()}-${S4()}-${S4()}${S4()}${S4()}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a promise that resolves after the given time.
|
||||
* @param {int} timeMs
|
||||
* @returns {Promise}
|
||||
*/
|
||||
export function wait(timeMs) {
|
||||
return new Promise(res => {
|
||||
setTimeout(res, timeMs);
|
||||
});
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import * as DrawIO from '../services/drawio';
|
||||
import {wait} from '../services/util';
|
||||
|
||||
let pageEditor = null;
|
||||
let currentNode = null;
|
||||
@ -57,24 +58,27 @@ async function updateContent(pngData) {
|
||||
});
|
||||
} catch (err) {
|
||||
handleUploadError(err);
|
||||
throw new Error(`Failed to save image with error: ${err}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
setTimeout(async () => {
|
||||
pageEditor.insertContent(`<div drawio-diagram contenteditable="false"><img src="${loadingImage}" id="${id}"></div>`);
|
||||
DrawIO.close();
|
||||
try {
|
||||
const img = await DrawIO.upload(pngData, options.pageId);
|
||||
pageEditor.undoManager.transact(() => {
|
||||
pageEditor.dom.setAttrib(id, 'src', img.url);
|
||||
pageEditor.dom.get(id).parentNode.setAttribute('drawio-diagram', img.id);
|
||||
});
|
||||
} catch (err) {
|
||||
pageEditor.dom.remove(id);
|
||||
handleUploadError(err);
|
||||
}
|
||||
}, 5);
|
||||
await wait(5);
|
||||
|
||||
pageEditor.insertContent(`<div drawio-diagram contenteditable="false"><img src="${loadingImage}" id="${id}"></div>`);
|
||||
DrawIO.close();
|
||||
|
||||
try {
|
||||
const img = await DrawIO.upload(pngData, options.pageId);
|
||||
pageEditor.undoManager.transact(() => {
|
||||
pageEditor.dom.setAttrib(id, 'src', img.url);
|
||||
pageEditor.dom.get(id).parentNode.setAttribute('drawio-diagram', img.id);
|
||||
});
|
||||
} catch (err) {
|
||||
pageEditor.dom.remove(id);
|
||||
handleUploadError(err);
|
||||
throw new Error(`Failed to save image with error: ${err}`);
|
||||
}
|
||||
}
|
||||
|
||||
function drawingInit() {
|
||||
|
Reference in New Issue
Block a user