1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/ReactNativeClient/lib/renderers/webviewLib.js

115 lines
3.3 KiB
JavaScript
Raw Normal View History

const webviewLib = {};
let manualDownloadResourceElements = [];
webviewLib.onUnloadedResourceClick = function(event) {
const resourceId = event.currentTarget.getAttribute('data-resource-id');
2019-09-19 23:51:18 +02:00
webviewLib.options_.postMessage(`markForDownload:${resourceId}`);
2019-07-29 15:43:53 +02:00
};
webviewLib.setupResourceManualDownload = function() {
for (const element of manualDownloadResourceElements) {
element.style.cursor = 'default';
element.removeEventListener('click', webviewLib.onUnloadedResourceClick);
}
manualDownloadResourceElements = [];
const elements = document.getElementsByClassName('resource-status-notDownloaded');
for (const element of elements) {
element.style.cursor = 'pointer';
element.addEventListener('click', webviewLib.onUnloadedResourceClick);
manualDownloadResourceElements.push(element);
}
2019-07-29 15:43:53 +02:00
};
webviewLib.handleInternalLink = function(event, anchorNode) {
const href = anchorNode.getAttribute('href');
if (!href) return false;
if (href.indexOf('#') === 0) {
event.preventDefault();
// let old_hash = location.hash;
location.hash = href;
2019-07-29 15:43:53 +02:00
// HACK
// For some reason anchors at the bottom cause the webview to move itself
// so that the content is aligned with the top of the screen
// This basically refreshes the scroll view so that is returns to a normal
// position, the scroll positions stays correct though
// Additionally an anchor could not be clicked twice because the location
// would not change, this fixes that also
//
// Commented out to fix https://github.com/laurent22/joplin/issues/2141
// We might need to fix a better fix to the previous bug.
//
// setTimeout(function() {
// location.hash = old_hash;
// }, 10);
return true;
}
2019-07-29 15:43:53 +02:00
return false;
2019-07-29 15:43:53 +02:00
};
webviewLib.getParentAnchorElement = function(element) {
let counter = 0;
while (true) {
if (counter++ >= 10000) {
2019-07-29 15:43:53 +02:00
console.warn('been looping for too long - exiting');
return null;
}
if (!element) return null;
if (element.nodeName === 'A') return element;
element = element.parentElement;
}
2019-07-29 15:43:53 +02:00
};
webviewLib.cloneError = function(error) {
return {
message: error.message,
2019-07-29 15:43:53 +02:00
stack: error.stack,
};
2019-07-29 15:43:53 +02:00
};
webviewLib.logEnabledEventHandler = function(fn) {
return function(event) {
try {
return fn(event);
} catch (error) {
2019-09-19 23:51:18 +02:00
webviewLib.options_.postMessage(`error:${JSON.stringify(webviewLib.cloneError(error))}`);
throw error;
}
2019-07-29 15:43:53 +02:00
};
};
webviewLib.initialize = function(options) {
webviewLib.options_ = options;
2019-07-29 15:43:53 +02:00
};
document.addEventListener('click', function(event) {
const anchor = webviewLib.getParentAnchorElement(event.target);
if (!anchor) return;
// Prevent URLs added via <a> tags from being opened within the application itself
// otherwise it would open the whole website within the WebView.
// Note that we already handle some links in html_inline.js, however not all of them
// go through this plugin, in particular links coming from third-party packages such
// as Katex.
if (!anchor.hasAttribute('data-from-md')) {
if (webviewLib.handleInternalLink(event, anchor)) return;
event.preventDefault();
if (anchor.getAttribute('href')) webviewLib.options_.postMessage(anchor.getAttribute('href'));
return;
}
// If this is an internal link, jump to the anchor directly
if (anchor.hasAttribute('data-from-md')) {
if (webviewLib.handleInternalLink(event, anchor)) return;
}
});