mirror of https://github.com/laurent22/joplin.git
Merge 7eecbe3d04
into f71ffa1644
commit
df0fd3b38a
|
@ -1,75 +1,40 @@
|
|||
// 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() {
|
||||
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) {
|
||||
if (document.readyState === 'complete' || document.readyState === 'interactive') {
|
||||
setTimeout(fn, 1);
|
||||
|
@ -86,6 +51,39 @@ const webviewApi = {
|
|||
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(() => {
|
||||
const rootElement = document.createElement('div');
|
||||
rootElement.setAttribute('id', 'joplin-plugin-content-root');
|
||||
|
|
Loading…
Reference in New Issue