You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-08-10 22:11:50 +02:00
Desktop: Plugin API: Fix compatibility with YesYouKan plugin (#12132)
This commit is contained in:
@@ -1,75 +1,40 @@
|
|||||||
// This is the API that JS files loaded from the webview can see
|
// This is the API that JS files loaded from the webview can see
|
||||||
const webviewApiPromises_ = {};
|
|
||||||
let viewMessageHandler_ = () => {};
|
|
||||||
const postMessage = (message) => {
|
|
||||||
parent.postMessage(message, '*');
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
function serializeForm(form) {
|
|
||||||
const output = {};
|
|
||||||
const formData = new FormData(form);
|
|
||||||
for (const key of formData.keys()) {
|
|
||||||
output[key] = formData.get(key);
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
function serializeForms(document) {
|
|
||||||
const forms = document.getElementsByTagName('form');
|
|
||||||
const output = {};
|
|
||||||
let untitledIndex = 0;
|
|
||||||
|
|
||||||
for (const form of forms) {
|
|
||||||
const name = `${form.getAttribute('name')}` || (`form${untitledIndex++}`);
|
|
||||||
output[name] = serializeForm(form);
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
function watchElementSize(element, onChange) {
|
|
||||||
const emitSizeChange = () => {
|
|
||||||
onChange(element.getBoundingClientRect());
|
|
||||||
};
|
|
||||||
const observer = new ResizeObserver(emitSizeChange);
|
|
||||||
observer.observe(element);
|
|
||||||
|
|
||||||
// Initial size
|
|
||||||
requestAnimationFrame(emitSizeChange);
|
|
||||||
}
|
|
||||||
|
|
||||||
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
|
|
||||||
const webviewApi = {
|
|
||||||
postMessage: function(message) {
|
|
||||||
const messageId = `userWebview_${Date.now()}${Math.random()}`;
|
|
||||||
|
|
||||||
const promise = new Promise((resolve, reject) => {
|
|
||||||
webviewApiPromises_[messageId] = { resolve, reject };
|
|
||||||
});
|
|
||||||
|
|
||||||
postMessage({
|
|
||||||
target: 'postMessageService.message',
|
|
||||||
message: {
|
|
||||||
from: 'userWebview',
|
|
||||||
to: 'plugin',
|
|
||||||
id: messageId,
|
|
||||||
content: message,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return promise;
|
|
||||||
},
|
|
||||||
|
|
||||||
onMessage: function(viewMessageHandler) {
|
|
||||||
viewMessageHandler_ = viewMessageHandler;
|
|
||||||
postMessage({
|
|
||||||
target: 'postMessageService.registerViewMessageHandler',
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
const webviewApiPromises_ = {};
|
||||||
|
let viewMessageHandler_ = () => {};
|
||||||
|
const postMessage = (message) => {
|
||||||
|
parent.postMessage(message, '*');
|
||||||
|
};
|
||||||
|
|
||||||
|
window.webviewApi = {
|
||||||
|
postMessage: function(message) {
|
||||||
|
const messageId = `userWebview_${Date.now()}${Math.random()}`;
|
||||||
|
|
||||||
|
const promise = new Promise((resolve, reject) => {
|
||||||
|
webviewApiPromises_[messageId] = { resolve, reject };
|
||||||
|
});
|
||||||
|
|
||||||
|
postMessage({
|
||||||
|
target: 'postMessageService.message',
|
||||||
|
message: {
|
||||||
|
from: 'userWebview',
|
||||||
|
to: 'plugin',
|
||||||
|
id: messageId,
|
||||||
|
content: message,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return promise;
|
||||||
|
},
|
||||||
|
|
||||||
|
onMessage: function(viewMessageHandler) {
|
||||||
|
viewMessageHandler_ = viewMessageHandler;
|
||||||
|
postMessage({
|
||||||
|
target: 'postMessageService.registerViewMessageHandler',
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
function docReady(fn) {
|
function docReady(fn) {
|
||||||
if (document.readyState === 'complete' || document.readyState === 'interactive') {
|
if (document.readyState === 'complete' || document.readyState === 'interactive') {
|
||||||
setTimeout(fn, 1);
|
setTimeout(fn, 1);
|
||||||
@@ -86,6 +51,39 @@ const webviewApi = {
|
|||||||
return output[output.length - 1];
|
return output[output.length - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function serializeForm(form) {
|
||||||
|
const output = {};
|
||||||
|
const formData = new FormData(form);
|
||||||
|
for (const key of formData.keys()) {
|
||||||
|
output[key] = formData.get(key);
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
function serializeForms(document) {
|
||||||
|
const forms = document.getElementsByTagName('form');
|
||||||
|
const output = {};
|
||||||
|
let untitledIndex = 0;
|
||||||
|
|
||||||
|
for (const form of forms) {
|
||||||
|
const name = `${form.getAttribute('name')}` || (`form${untitledIndex++}`);
|
||||||
|
output[name] = serializeForm(form);
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
function watchElementSize(element, onChange) {
|
||||||
|
const emitSizeChange = () => {
|
||||||
|
onChange(element.getBoundingClientRect());
|
||||||
|
};
|
||||||
|
const observer = new ResizeObserver(emitSizeChange);
|
||||||
|
observer.observe(element);
|
||||||
|
|
||||||
|
// Initial size
|
||||||
|
requestAnimationFrame(emitSizeChange);
|
||||||
|
}
|
||||||
|
|
||||||
docReady(() => {
|
docReady(() => {
|
||||||
const rootElement = document.createElement('div');
|
const rootElement = document.createElement('div');
|
||||||
rootElement.setAttribute('id', 'joplin-plugin-content-root');
|
rootElement.setAttribute('id', 'joplin-plugin-content-root');
|
||||||
|
Reference in New Issue
Block a user