feat: edit print templates on the printer page and store them on presets
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,6 +2,101 @@
|
||||
// Handles both HTML tables (from Google Sheets) and tab-separated values
|
||||
|
||||
window.markdownTablePaste = {
|
||||
findEditor: function(editorId) {
|
||||
let textarea = null;
|
||||
let codeMirror = null;
|
||||
|
||||
if (editorId) {
|
||||
const editorElement = document.getElementById(editorId);
|
||||
if (editorElement) {
|
||||
textarea = editorElement.querySelector('textarea');
|
||||
const cmEl = editorElement.querySelector('.CodeMirror');
|
||||
if (cmEl && cmEl.CodeMirror) {
|
||||
codeMirror = cmEl.CodeMirror;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const containers = document.querySelectorAll('.EasyMDEContainer');
|
||||
if (containers.length > 0) {
|
||||
const lastContainer = containers[containers.length - 1];
|
||||
textarea = lastContainer.querySelector('textarea');
|
||||
const cmEl = lastContainer.querySelector('.CodeMirror');
|
||||
if (cmEl && cmEl.CodeMirror) {
|
||||
codeMirror = cmEl.CodeMirror;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!codeMirror && textarea && window.EasyMDE) {
|
||||
const easyMDEInstances = window.EasyMDE.instances || [];
|
||||
for (let i = 0; i < easyMDEInstances.length; i++) {
|
||||
const instance = easyMDEInstances[i];
|
||||
if (instance && instance.codemirror) {
|
||||
const cmTextarea = instance.codemirror.getTextArea();
|
||||
if (cmTextarea === textarea) {
|
||||
codeMirror = instance.codemirror;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!codeMirror && textarea.parentElement) {
|
||||
const parent = textarea.parentElement;
|
||||
if (parent._easyMDEInstance && parent._easyMDEInstance.codemirror) {
|
||||
codeMirror = parent._easyMDEInstance.codemirror;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { textarea, codeMirror };
|
||||
},
|
||||
|
||||
getValue: function(editorId) {
|
||||
const found = this.findEditor(editorId);
|
||||
if (found.codeMirror) {
|
||||
return found.codeMirror.getValue();
|
||||
}
|
||||
if (found.textarea) {
|
||||
return found.textarea.value;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
setValue: function(editorId, text) {
|
||||
const found = this.findEditor(editorId);
|
||||
if (found.codeMirror) {
|
||||
found.codeMirror.setValue(text ?? '');
|
||||
return true;
|
||||
}
|
||||
if (found.textarea) {
|
||||
found.textarea.value = text ?? '';
|
||||
found.textarea.dispatchEvent(new Event('input', { bubbles: true }));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
insertAtCursor: function(editorId, text) {
|
||||
const found = this.findEditor(editorId);
|
||||
if (found.codeMirror) {
|
||||
found.codeMirror.replaceSelection(text);
|
||||
found.codeMirror.focus();
|
||||
return true;
|
||||
}
|
||||
if (found.textarea) {
|
||||
const start = found.textarea.selectionStart || 0;
|
||||
const end = found.textarea.selectionEnd || 0;
|
||||
const before = found.textarea.value.substring(0, start);
|
||||
const after = found.textarea.value.substring(end);
|
||||
found.textarea.value = before + text + after;
|
||||
found.textarea.selectionStart = found.textarea.selectionEnd = before.length + text.length;
|
||||
found.textarea.dispatchEvent(new Event('input', { bubbles: true }));
|
||||
found.textarea.focus();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Initializes paste handler for a MarkdownEditor instance.
|
||||
* Finds the textarea or CodeMirror instance created by EasyMDE and attaches paste event handler.
|
||||
@@ -13,78 +108,11 @@ window.markdownTablePaste = {
|
||||
|
||||
const tryInitialize = function() {
|
||||
attempts++;
|
||||
|
||||
let textarea = null;
|
||||
let codeMirror = null;
|
||||
|
||||
// Try to find the textarea element
|
||||
if (editorId) {
|
||||
const editorElement = document.getElementById(editorId);
|
||||
if (editorElement) {
|
||||
textarea = editorElement.querySelector('textarea');
|
||||
}
|
||||
} else {
|
||||
// Find the most recently created EasyMDE textarea (last one in DOM)
|
||||
const containers = document.querySelectorAll('.EasyMDEContainer');
|
||||
if (containers.length > 0) {
|
||||
const lastContainer = containers[containers.length - 1];
|
||||
textarea = lastContainer.querySelector('textarea');
|
||||
}
|
||||
|
||||
// Fallback: try to find any textarea near an editor-toolbar
|
||||
if (!textarea) {
|
||||
const toolbars = document.querySelectorAll('.editor-toolbar');
|
||||
if (toolbars.length > 0) {
|
||||
const lastToolbar = toolbars[toolbars.length - 1];
|
||||
const nextSibling = lastToolbar.nextElementSibling;
|
||||
if (nextSibling && nextSibling.tagName === 'TEXTAREA') {
|
||||
textarea = nextSibling;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Another fallback: find any textarea that's a child of a container with editor classes
|
||||
if (!textarea) {
|
||||
const allTextareas = document.querySelectorAll('textarea');
|
||||
for (let ta of allTextareas) {
|
||||
const parent = ta.parentElement;
|
||||
if (parent && (
|
||||
parent.classList.contains('EasyMDEContainer') ||
|
||||
parent.classList.contains('editor') ||
|
||||
parent.querySelector('.editor-toolbar')
|
||||
)) {
|
||||
textarea = ta;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we found a textarea, try to get the CodeMirror instance from EasyMDE
|
||||
if (textarea) {
|
||||
if (window.EasyMDE) {
|
||||
const easyMDEInstances = window.EasyMDE.instances || [];
|
||||
|
||||
for (let i = 0; i < easyMDEInstances.length; i++) {
|
||||
const instance = easyMDEInstances[i];
|
||||
if (instance && instance.codemirror) {
|
||||
const cmTextarea = instance.codemirror.getTextArea();
|
||||
if (cmTextarea === textarea) {
|
||||
codeMirror = instance.codemirror;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Alternative: try to get CodeMirror from the textarea's parent
|
||||
if (!codeMirror && textarea.parentElement) {
|
||||
const parent = textarea.parentElement;
|
||||
if (parent._easyMDEInstance && parent._easyMDEInstance.codemirror) {
|
||||
codeMirror = parent._easyMDEInstance.codemirror;
|
||||
}
|
||||
}
|
||||
}
|
||||
const found = window.markdownTablePaste.findEditor(editorId);
|
||||
const textarea = found.textarea;
|
||||
const codeMirror = found.codeMirror;
|
||||
|
||||
if (textarea || codeMirror) {
|
||||
const target = codeMirror || textarea;
|
||||
|
||||
if (target) {
|
||||
|
||||
Reference in New Issue
Block a user