refactor: externalize editor upload context
This commit is contained in:
@@ -20,6 +20,110 @@ function currentRootUri() {
|
||||
: '';
|
||||
}
|
||||
|
||||
function editorUploadRootFromElementWithDeps(target, deps) {
|
||||
if (deps && typeof deps.editorUploadRootFromElement === 'function') {
|
||||
return deps.editorUploadRootFromElement(target);
|
||||
}
|
||||
if (!(target instanceof Element)) return null;
|
||||
var selector = String(deps && deps.rootSelector || '[data-editor-host-kind="leptos_tiptap_island"], [data-editor-host-kind="leptos_tiptap_resource"]');
|
||||
var root = target.closest(selector);
|
||||
return root instanceof HTMLElement ? root : null;
|
||||
}
|
||||
|
||||
function resolveEditorUploadContext(detail, deps) {
|
||||
var root = null;
|
||||
var selector = detail && detail.editorRootSelector ? String(detail.editorRootSelector) : '';
|
||||
if (selector) {
|
||||
try {
|
||||
var selected = document.querySelector(selector);
|
||||
if (selected instanceof HTMLElement) root = selected;
|
||||
} catch (_) {}
|
||||
}
|
||||
if (!root && window.__mnoteIntendedSlashRoot instanceof HTMLElement && window.__mnoteIntendedSlashRoot.isConnected) {
|
||||
root = window.__mnoteIntendedSlashRoot;
|
||||
}
|
||||
if (!root && window.__mnoteLastEditorUploadRoot instanceof HTMLElement && window.__mnoteLastEditorUploadRoot.isConnected) {
|
||||
root = window.__mnoteLastEditorUploadRoot;
|
||||
}
|
||||
if (!root && document.activeElement instanceof Element) {
|
||||
root = editorUploadRootFromElementWithDeps(document.activeElement, deps);
|
||||
}
|
||||
if (!root) {
|
||||
var rootSelector = String(deps && deps.rootSelector || '[data-editor-host-kind="leptos_tiptap_island"], [data-editor-host-kind="leptos_tiptap_resource"]');
|
||||
var focused = document.querySelector(rootSelector + ' .ProseMirror:focus-within');
|
||||
root = editorUploadRootFromElementWithDeps(focused, deps);
|
||||
}
|
||||
if (!root) {
|
||||
root = document.querySelector('[data-editor-host-kind="leptos_tiptap_island"][data-pane-role="primary"]');
|
||||
}
|
||||
var pane = root instanceof Element ? root.closest('.document-pane[data-pane-role]') : null;
|
||||
var shell = root instanceof Element ? root.closest('.document-shell[data-document-id]') : null;
|
||||
var currentDocumentId = deps && typeof deps.currentDocumentId === 'function' ? deps.currentDocumentId : function() { return ''; };
|
||||
var resolveWorkspaceId = deps && typeof deps.resolveWorkspaceId === 'function' ? deps.resolveWorkspaceId : function() { return ''; };
|
||||
return {
|
||||
root: root instanceof HTMLElement ? root : null,
|
||||
documentId: String(
|
||||
detail && detail.documentId
|
||||
|| (root instanceof HTMLElement && root.getAttribute('data-document-id'))
|
||||
|| (pane instanceof HTMLElement && pane.getAttribute('data-pane-document-id'))
|
||||
|| (shell instanceof HTMLElement && shell.getAttribute('data-document-id'))
|
||||
|| currentDocumentId()
|
||||
|| ''
|
||||
).trim(),
|
||||
workspaceId: String(
|
||||
detail && detail.workspaceId
|
||||
|| (root instanceof HTMLElement && root.getAttribute('data-workspace-id'))
|
||||
|| (pane instanceof HTMLElement && pane.getAttribute('data-pane-workspace-id'))
|
||||
|| (shell instanceof HTMLElement && shell.getAttribute('data-workspace-id'))
|
||||
|| resolveWorkspaceId(document.body)
|
||||
|| ''
|
||||
).trim()
|
||||
};
|
||||
}
|
||||
|
||||
function editorRootFromUploadOptions(options, deps) {
|
||||
if (options && options.editorRoot instanceof HTMLElement) return options.editorRoot;
|
||||
if (window.__mnoteIntendedSlashRoot instanceof HTMLElement && window.__mnoteIntendedSlashRoot.isConnected) {
|
||||
return window.__mnoteIntendedSlashRoot;
|
||||
}
|
||||
if (window.__mnoteLastEditorUploadRoot instanceof HTMLElement && window.__mnoteLastEditorUploadRoot.isConnected) {
|
||||
return window.__mnoteLastEditorUploadRoot;
|
||||
}
|
||||
var rootSelector = String(deps && deps.rootSelector || '[data-editor-host-kind="leptos_tiptap_island"], [data-editor-host-kind="leptos_tiptap_resource"]');
|
||||
var focused = document.querySelector(rootSelector + ' .ProseMirror:focus-within');
|
||||
return editorUploadRootFromElementWithDeps(focused, deps);
|
||||
}
|
||||
|
||||
function openEditorUploadFilePicker(detail, deps) {
|
||||
var uploadContext = resolveEditorUploadContext(detail || {}, deps || {});
|
||||
var input = document.createElement('input');
|
||||
input.type = 'file';
|
||||
input.multiple = detail && detail.multiple !== false;
|
||||
if (detail && detail.accept) input.accept = String(detail.accept);
|
||||
input.style.position = 'fixed';
|
||||
input.style.left = '-9999px';
|
||||
input.style.top = '-9999px';
|
||||
document.body.appendChild(input);
|
||||
input.addEventListener('change', function() {
|
||||
var files = Array.from(input.files || []);
|
||||
input.remove();
|
||||
var resolveWorkspaceId = deps && typeof deps.resolveWorkspaceId === 'function' ? deps.resolveWorkspaceId : function() { return ''; };
|
||||
var currentDocumentId = deps && typeof deps.currentDocumentId === 'function' ? deps.currentDocumentId : function() { return ''; };
|
||||
var uploadFilesWithResolvedTarget = deps && typeof deps.uploadFilesWithResolvedTarget === 'function' ? deps.uploadFilesWithResolvedTarget : null;
|
||||
if (!uploadFilesWithResolvedTarget) return;
|
||||
void uploadFilesWithResolvedTarget(files, {
|
||||
workspaceId: uploadContext.workspaceId || resolveWorkspaceId(document.body),
|
||||
documentId: uploadContext.documentId || currentDocumentId(),
|
||||
targetRowId: null,
|
||||
uploadIntent: 'editor.markdown.attach'
|
||||
}, {
|
||||
insertIntoEditor: detail && detail.insertIntoEditor !== false,
|
||||
editorRoot: uploadContext.root
|
||||
});
|
||||
}, { once: true });
|
||||
input.click();
|
||||
}
|
||||
|
||||
function localAssetOpenUrl(asset, download, context) {
|
||||
if (!isLocalUploadedAsset(asset)) return '';
|
||||
var rootUri = String(context && context.rootUri || asset && (asset.rootUri || asset.root_uri) || '').trim() || currentRootUri();
|
||||
@@ -145,6 +249,9 @@ function uploadedFileSize(asset) {
|
||||
window.__mnoteLocalUploadRuntime = {
|
||||
uploadedAssetTitle: uploadedAssetTitle,
|
||||
uploadedAssetUrl: uploadedAssetUrl,
|
||||
resolveEditorUploadContext: resolveEditorUploadContext,
|
||||
editorRootFromUploadOptions: editorRootFromUploadOptions,
|
||||
openEditorUploadFilePicker: openEditorUploadFilePicker,
|
||||
localAssetOpenUrl: localAssetOpenUrl,
|
||||
uploadedAssetType: uploadedAssetType,
|
||||
fileTreeIconKindForFileName: fileTreeIconKindForFileName,
|
||||
|
||||
Reference in New Issue
Block a user