2023-04-03 04:06:57 -06:00
|
|
|
import '@github/markdown-toolbar-element';
|
2023-04-09 10:18:45 -06:00
|
|
|
import '@github/text-expander-element';
|
2023-04-07 11:03:29 -06:00
|
|
|
import $ from 'jquery';
|
2024-07-07 09:32:30 -06:00
|
|
|
import {attachTribute} from '../tribute.ts';
|
|
|
|
import {hideElem, showElem, autosize, isElemVisible} from '../../utils/dom.ts';
|
2024-10-22 20:48:04 -06:00
|
|
|
import {
|
|
|
|
EventUploadStateChanged,
|
|
|
|
initEasyMDEPaste,
|
|
|
|
initTextareaEvents,
|
|
|
|
triggerUploadStateChanged,
|
|
|
|
} from './EditorUpload.ts';
|
2024-07-07 09:32:30 -06:00
|
|
|
import {handleGlobalEnterQuickSubmit} from './QuickSubmit.ts';
|
|
|
|
import {renderPreviewPanelContent} from '../repo-editor.ts';
|
|
|
|
import {easyMDEToolbarActions} from './EasyMDEToolbarActions.ts';
|
|
|
|
import {initTextExpander} from './TextExpander.ts';
|
|
|
|
import {showErrorToast} from '../../modules/toast.ts';
|
|
|
|
import {POST} from '../../modules/fetch.ts';
|
2024-11-04 03:14:36 -07:00
|
|
|
import {
|
|
|
|
EventEditorContentChanged,
|
|
|
|
initTextareaMarkdown,
|
|
|
|
textareaInsertText,
|
|
|
|
triggerEditorContentChanged,
|
|
|
|
} from './EditorMarkdown.ts';
|
2024-07-07 09:32:30 -06:00
|
|
|
import {DropzoneCustomEventReloadFiles, initDropzone} from '../dropzone.ts';
|
2024-11-04 03:14:36 -07:00
|
|
|
import {createTippy} from '../../modules/tippy.ts';
|
2023-04-03 04:06:57 -06:00
|
|
|
|
|
|
|
let elementIdCounter = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* validate if the given textarea is non-empty.
|
2024-02-20 03:37:37 -07:00
|
|
|
* @param {HTMLElement} textarea - The textarea element to be validated.
|
2023-04-03 04:06:57 -06:00
|
|
|
* @returns {boolean} returns true if validation succeeded.
|
|
|
|
*/
|
2024-02-20 03:37:37 -07:00
|
|
|
export function validateTextareaNonEmpty(textarea) {
|
2023-04-03 04:06:57 -06:00
|
|
|
// When using EasyMDE, the original edit area HTML element is hidden, breaking HTML5 input validation.
|
|
|
|
// The workaround (https://github.com/sparksuite/simplemde-markdown-editor/issues/324) doesn't work with contenteditable, so we just show an alert.
|
2024-02-20 03:37:37 -07:00
|
|
|
if (!textarea.value) {
|
|
|
|
if (isElemVisible(textarea)) {
|
|
|
|
textarea.required = true;
|
|
|
|
const form = textarea.closest('form');
|
|
|
|
form?.reportValidity();
|
2023-04-03 04:06:57 -06:00
|
|
|
} else {
|
|
|
|
// The alert won't hurt users too much, because we are dropping the EasyMDE and the check only occurs in a few places.
|
2023-06-26 20:45:24 -06:00
|
|
|
showErrorToast('Require non-empty content');
|
2023-04-03 04:06:57 -06:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-10-22 20:48:04 -06:00
|
|
|
export class ComboMarkdownEditor {
|
|
|
|
static EventEditorContentChanged = EventEditorContentChanged;
|
|
|
|
static EventUploadStateChanged = EventUploadStateChanged;
|
|
|
|
|
|
|
|
public container : HTMLElement;
|
|
|
|
|
|
|
|
// TODO: use correct types to replace these "any" types
|
|
|
|
options: any;
|
|
|
|
|
|
|
|
tabEditor: HTMLElement;
|
|
|
|
tabPreviewer: HTMLElement;
|
|
|
|
|
|
|
|
easyMDE: any;
|
|
|
|
easyMDEToolbarActions: any;
|
|
|
|
easyMDEToolbarDefault: any;
|
|
|
|
|
|
|
|
textarea: HTMLTextAreaElement & {_giteaComboMarkdownEditor: any};
|
|
|
|
textareaMarkdownToolbar: HTMLElement;
|
|
|
|
textareaAutosize: any;
|
|
|
|
|
|
|
|
dropzone: HTMLElement;
|
|
|
|
attachedDropzoneInst: any;
|
|
|
|
|
|
|
|
previewUrl: string;
|
|
|
|
previewContext: string;
|
|
|
|
previewMode: string;
|
|
|
|
|
2023-04-03 04:06:57 -06:00
|
|
|
constructor(container, options = {}) {
|
|
|
|
container._giteaComboMarkdownEditor = this;
|
|
|
|
this.options = options;
|
|
|
|
this.container = container;
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2023-04-07 11:03:29 -06:00
|
|
|
this.prepareEasyMDEToolbarActions();
|
2023-05-08 16:22:52 -06:00
|
|
|
this.setupContainer();
|
2023-04-07 11:03:29 -06:00
|
|
|
this.setupTab();
|
2024-06-26 11:01:20 -06:00
|
|
|
await this.setupDropzone(); // textarea depends on dropzone
|
2023-04-07 11:03:29 -06:00
|
|
|
this.setupTextarea();
|
|
|
|
|
2023-05-08 16:22:52 -06:00
|
|
|
await this.switchToUserPreference();
|
2023-04-07 11:03:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
applyEditorHeights(el, heights) {
|
|
|
|
if (!heights) return;
|
|
|
|
if (heights.minHeight) el.style.minHeight = heights.minHeight;
|
|
|
|
if (heights.height) el.style.height = heights.height;
|
|
|
|
if (heights.maxHeight) el.style.maxHeight = heights.maxHeight;
|
|
|
|
}
|
|
|
|
|
2023-05-08 16:22:52 -06:00
|
|
|
setupContainer() {
|
|
|
|
initTextExpander(this.container.querySelector('text-expander'));
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:03:29 -06:00
|
|
|
setupTextarea() {
|
2023-04-03 04:06:57 -06:00
|
|
|
this.textarea = this.container.querySelector('.markdown-text-editor');
|
|
|
|
this.textarea._giteaComboMarkdownEditor = this;
|
2023-04-07 11:03:29 -06:00
|
|
|
this.textarea.id = `_combo_markdown_editor_${String(elementIdCounter++)}`;
|
2024-10-22 20:48:04 -06:00
|
|
|
this.textarea.addEventListener('input', () => triggerEditorContentChanged(this.container));
|
2023-04-07 11:03:29 -06:00
|
|
|
this.applyEditorHeights(this.textarea, this.options.editorHeights);
|
2023-07-30 16:11:15 -06:00
|
|
|
|
|
|
|
if (this.textarea.getAttribute('data-disable-autosize') !== 'true') {
|
|
|
|
this.textareaAutosize = autosize(this.textarea, {viewportMarginBottom: 130});
|
|
|
|
}
|
2023-04-07 11:03:29 -06:00
|
|
|
|
2023-04-03 04:06:57 -06:00
|
|
|
this.textareaMarkdownToolbar = this.container.querySelector('markdown-toolbar');
|
|
|
|
this.textareaMarkdownToolbar.setAttribute('for', this.textarea.id);
|
2023-04-11 02:36:18 -06:00
|
|
|
for (const el of this.textareaMarkdownToolbar.querySelectorAll('.markdown-toolbar-button')) {
|
|
|
|
// upstream bug: The role code is never executed in base MarkdownButtonElement https://github.com/github/markdown-toolbar-element/issues/70
|
|
|
|
el.setAttribute('role', 'button');
|
2023-08-15 05:31:48 -06:00
|
|
|
// the editor usually is in a form, so the buttons should have "type=button", avoiding conflicting with the form's submit.
|
|
|
|
if (el.nodeName === 'BUTTON' && !el.getAttribute('type')) el.setAttribute('type', 'button');
|
2023-04-11 02:36:18 -06:00
|
|
|
}
|
2023-04-13 13:05:06 -06:00
|
|
|
|
|
|
|
const monospaceButton = this.container.querySelector('.markdown-switch-monospace');
|
|
|
|
const monospaceEnabled = localStorage?.getItem('markdown-editor-monospace') === 'true';
|
|
|
|
const monospaceText = monospaceButton.getAttribute(monospaceEnabled ? 'data-disable-text' : 'data-enable-text');
|
|
|
|
monospaceButton.setAttribute('data-tooltip-content', monospaceText);
|
|
|
|
monospaceButton.setAttribute('aria-checked', String(monospaceEnabled));
|
2024-11-04 03:14:36 -07:00
|
|
|
monospaceButton.addEventListener('click', (e) => {
|
2023-04-13 13:05:06 -06:00
|
|
|
e.preventDefault();
|
|
|
|
const enabled = localStorage?.getItem('markdown-editor-monospace') !== 'true';
|
|
|
|
localStorage.setItem('markdown-editor-monospace', String(enabled));
|
2024-03-28 02:31:07 -06:00
|
|
|
this.textarea.classList.toggle('tw-font-mono', enabled);
|
2023-04-13 13:05:06 -06:00
|
|
|
const text = monospaceButton.getAttribute(enabled ? 'data-disable-text' : 'data-enable-text');
|
|
|
|
monospaceButton.setAttribute('data-tooltip-content', text);
|
|
|
|
monospaceButton.setAttribute('aria-checked', String(enabled));
|
|
|
|
});
|
|
|
|
|
|
|
|
const easymdeButton = this.container.querySelector('.markdown-switch-easymde');
|
2024-11-04 03:14:36 -07:00
|
|
|
easymdeButton.addEventListener('click', async (e) => {
|
2023-04-03 04:06:57 -06:00
|
|
|
e.preventDefault();
|
2023-04-07 11:03:29 -06:00
|
|
|
this.userPreferredEditor = 'easymde';
|
2023-04-03 04:06:57 -06:00
|
|
|
await this.switchToEasyMDE();
|
|
|
|
});
|
|
|
|
|
2024-11-04 03:14:36 -07:00
|
|
|
this.initMarkdownButtonTableAdd();
|
|
|
|
|
2024-06-21 02:14:40 -06:00
|
|
|
initTextareaMarkdown(this.textarea);
|
2024-09-01 09:15:29 -06:00
|
|
|
initTextareaEvents(this.textarea, this.dropzone);
|
2023-04-07 11:03:29 -06:00
|
|
|
}
|
2023-04-03 04:06:57 -06:00
|
|
|
|
2024-06-26 11:01:20 -06:00
|
|
|
async setupDropzone() {
|
2023-04-03 04:06:57 -06:00
|
|
|
const dropzoneParentContainer = this.container.getAttribute('data-dropzone-parent-container');
|
2024-10-22 20:48:04 -06:00
|
|
|
if (!dropzoneParentContainer) return;
|
|
|
|
this.dropzone = this.container.closest(this.container.getAttribute('data-dropzone-parent-container'))?.querySelector('.dropzone');
|
|
|
|
if (!this.dropzone) return;
|
|
|
|
|
|
|
|
this.attachedDropzoneInst = await initDropzone(this.dropzone);
|
|
|
|
// dropzone events
|
|
|
|
// * "processing" means a file is being uploaded
|
|
|
|
// * "queuecomplete" means all files have been uploaded
|
|
|
|
this.attachedDropzoneInst.on('processing', () => triggerUploadStateChanged(this.container));
|
|
|
|
this.attachedDropzoneInst.on('queuecomplete', () => triggerUploadStateChanged(this.container));
|
2023-04-03 04:06:57 -06:00
|
|
|
}
|
|
|
|
|
2024-06-26 11:01:20 -06:00
|
|
|
dropzoneGetFiles() {
|
|
|
|
if (!this.dropzone) return null;
|
2024-10-22 20:48:04 -06:00
|
|
|
return Array.from(this.dropzone.querySelectorAll<HTMLInputElement>('.files [name=files]'), (el) => el.value);
|
2024-06-26 11:01:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
dropzoneReloadFiles() {
|
|
|
|
if (!this.dropzone) return;
|
2024-06-27 03:31:49 -06:00
|
|
|
this.attachedDropzoneInst.emit(DropzoneCustomEventReloadFiles);
|
2024-06-26 11:01:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
dropzoneSubmitReload() {
|
|
|
|
if (!this.dropzone) return;
|
|
|
|
this.attachedDropzoneInst.emit('submit');
|
2024-06-27 03:31:49 -06:00
|
|
|
this.attachedDropzoneInst.emit(DropzoneCustomEventReloadFiles);
|
2024-06-26 11:01:20 -06:00
|
|
|
}
|
|
|
|
|
2024-10-22 20:48:04 -06:00
|
|
|
isUploading() {
|
|
|
|
if (!this.dropzone) return false;
|
|
|
|
return this.attachedDropzoneInst.getQueuedFiles().length || this.attachedDropzoneInst.getUploadingFiles().length;
|
|
|
|
}
|
|
|
|
|
2023-04-03 04:06:57 -06:00
|
|
|
setupTab() {
|
2024-10-22 20:48:04 -06:00
|
|
|
const tabs = this.container.querySelectorAll<HTMLElement>('.tabular.menu > .item');
|
2023-04-03 04:06:57 -06:00
|
|
|
|
|
|
|
// Fomantic Tab requires the "data-tab" to be globally unique.
|
|
|
|
// So here it uses our defined "data-tab-for" and "data-tab-panel" to generate the "data-tab" attribute for Fomantic.
|
2024-06-22 03:25:04 -06:00
|
|
|
this.tabEditor = Array.from(tabs).find((tab) => tab.getAttribute('data-tab-for') === 'markdown-writer');
|
|
|
|
this.tabPreviewer = Array.from(tabs).find((tab) => tab.getAttribute('data-tab-for') === 'markdown-previewer');
|
|
|
|
this.tabEditor.setAttribute('data-tab', `markdown-writer-${elementIdCounter}`);
|
|
|
|
this.tabPreviewer.setAttribute('data-tab', `markdown-previewer-${elementIdCounter}`);
|
|
|
|
|
|
|
|
const panelEditor = this.container.querySelector('.ui.tab[data-tab-panel="markdown-writer"]');
|
|
|
|
const panelPreviewer = this.container.querySelector('.ui.tab[data-tab-panel="markdown-previewer"]');
|
2024-03-24 18:00:54 -06:00
|
|
|
panelEditor.setAttribute('data-tab', `markdown-writer-${elementIdCounter}`);
|
|
|
|
panelPreviewer.setAttribute('data-tab', `markdown-previewer-${elementIdCounter}`);
|
2023-04-03 04:06:57 -06:00
|
|
|
elementIdCounter++;
|
|
|
|
|
2024-06-22 03:25:04 -06:00
|
|
|
this.tabEditor.addEventListener('click', () => {
|
2023-08-24 23:26:32 -06:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
this.focus();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-03-24 18:00:54 -06:00
|
|
|
$(tabs).tab();
|
2023-04-03 04:06:57 -06:00
|
|
|
|
2024-06-22 03:25:04 -06:00
|
|
|
this.previewUrl = this.tabPreviewer.getAttribute('data-preview-url');
|
|
|
|
this.previewContext = this.tabPreviewer.getAttribute('data-preview-context');
|
2023-04-03 04:06:57 -06:00
|
|
|
this.previewMode = this.options.previewMode ?? 'comment';
|
2024-06-22 03:25:04 -06:00
|
|
|
this.tabPreviewer.addEventListener('click', async () => {
|
2024-02-24 21:42:29 -07:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('mode', this.previewMode);
|
|
|
|
formData.append('context', this.previewContext);
|
|
|
|
formData.append('text', this.value());
|
|
|
|
const response = await POST(this.previewUrl, {data: formData});
|
|
|
|
const data = await response.text();
|
2024-03-24 18:00:54 -06:00
|
|
|
renderPreviewPanelContent($(panelPreviewer), data);
|
2023-04-03 04:06:57 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-11-04 03:14:36 -07:00
|
|
|
generateMarkdownTable(rows: number, cols: number): string {
|
|
|
|
const tableLines = [];
|
|
|
|
tableLines.push(
|
|
|
|
`| ${'Header '.repeat(cols).trim().split(' ').join(' | ')} |`,
|
|
|
|
`| ${'--- '.repeat(cols).trim().split(' ').join(' | ')} |`,
|
|
|
|
);
|
|
|
|
for (let i = 0; i < rows; i++) {
|
|
|
|
tableLines.push(`| ${'Cell '.repeat(cols).trim().split(' ').join(' | ')} |`);
|
|
|
|
}
|
|
|
|
return tableLines.join('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
initMarkdownButtonTableAdd() {
|
|
|
|
const addTableButton = this.container.querySelector('.markdown-button-table-add');
|
|
|
|
const addTablePanel = this.container.querySelector('.markdown-add-table-panel');
|
|
|
|
// here the tippy can't attach to the button because the button already owns a tippy for tooltip
|
|
|
|
const addTablePanelTippy = createTippy(addTablePanel, {
|
|
|
|
content: addTablePanel,
|
|
|
|
trigger: 'manual',
|
|
|
|
placement: 'bottom',
|
|
|
|
hideOnClick: true,
|
|
|
|
interactive: true,
|
|
|
|
getReferenceClientRect: () => addTableButton.getBoundingClientRect(),
|
|
|
|
});
|
|
|
|
addTableButton.addEventListener('click', () => addTablePanelTippy.show());
|
|
|
|
|
|
|
|
addTablePanel.querySelector('.ui.button.primary').addEventListener('click', () => {
|
|
|
|
let rows = parseInt(addTablePanel.querySelector<HTMLInputElement>('[name=rows]').value);
|
|
|
|
let cols = parseInt(addTablePanel.querySelector<HTMLInputElement>('[name=cols]').value);
|
|
|
|
rows = Math.max(1, Math.min(100, rows));
|
|
|
|
cols = Math.max(1, Math.min(100, cols));
|
|
|
|
textareaInsertText(this.textarea, `\n${this.generateMarkdownTable(rows, cols)}\n\n`);
|
|
|
|
addTablePanelTippy.hide();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-22 03:25:04 -06:00
|
|
|
switchTabToEditor() {
|
|
|
|
this.tabEditor.click();
|
|
|
|
}
|
|
|
|
|
2023-04-03 04:06:57 -06:00
|
|
|
prepareEasyMDEToolbarActions() {
|
|
|
|
this.easyMDEToolbarDefault = [
|
2023-05-02 23:23:39 -06:00
|
|
|
'bold', 'italic', 'strikethrough', '|', 'heading-1', 'heading-2', 'heading-3',
|
|
|
|
'heading-bigger', 'heading-smaller', '|', 'code', 'quote', '|', 'gitea-checkbox-empty',
|
|
|
|
'gitea-checkbox-checked', '|', 'unordered-list', 'ordered-list', '|', 'link', 'image',
|
|
|
|
'table', 'horizontal-rule', '|', 'gitea-switch-to-textarea',
|
2023-04-03 04:06:57 -06:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-05-02 23:23:39 -06:00
|
|
|
parseEasyMDEToolbar(EasyMDE, actions) {
|
|
|
|
this.easyMDEToolbarActions = this.easyMDEToolbarActions || easyMDEToolbarActions(EasyMDE, this);
|
2023-04-03 04:06:57 -06:00
|
|
|
const processed = [];
|
|
|
|
for (const action of actions) {
|
2023-05-02 23:23:39 -06:00
|
|
|
const actionButton = this.easyMDEToolbarActions[action];
|
|
|
|
if (!actionButton) throw new Error(`Unknown EasyMDE toolbar action ${action}`);
|
|
|
|
processed.push(actionButton);
|
2023-04-03 04:06:57 -06:00
|
|
|
}
|
|
|
|
return processed;
|
|
|
|
}
|
|
|
|
|
2023-05-08 16:22:52 -06:00
|
|
|
async switchToUserPreference() {
|
|
|
|
if (this.userPreferredEditor === 'easymde') {
|
|
|
|
await this.switchToEasyMDE();
|
|
|
|
} else {
|
|
|
|
this.switchToTextarea();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:03:29 -06:00
|
|
|
switchToTextarea() {
|
2023-05-08 16:22:52 -06:00
|
|
|
if (!this.easyMDE) return;
|
2023-04-03 04:06:57 -06:00
|
|
|
showElem(this.textareaMarkdownToolbar);
|
|
|
|
if (this.easyMDE) {
|
|
|
|
this.easyMDE.toTextArea();
|
|
|
|
this.easyMDE = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async switchToEasyMDE() {
|
2023-05-08 16:22:52 -06:00
|
|
|
if (this.easyMDE) return;
|
2023-04-03 04:06:57 -06:00
|
|
|
// EasyMDE's CSS should be loaded via webpack config, otherwise our own styles can not overwrite the default styles.
|
|
|
|
const {default: EasyMDE} = await import(/* webpackChunkName: "easymde" */'easymde');
|
|
|
|
const easyMDEOpt = {
|
|
|
|
autoDownloadFontAwesome: false,
|
|
|
|
element: this.textarea,
|
|
|
|
forceSync: true,
|
|
|
|
renderingConfig: {singleLineBreaks: false},
|
|
|
|
indentWithTabs: false,
|
|
|
|
tabSize: 4,
|
|
|
|
spellChecker: false,
|
|
|
|
inputStyle: 'contenteditable', // nativeSpellcheck requires contenteditable
|
|
|
|
nativeSpellcheck: true,
|
|
|
|
...this.options.easyMDEOptions,
|
|
|
|
};
|
2023-05-02 23:23:39 -06:00
|
|
|
easyMDEOpt.toolbar = this.parseEasyMDEToolbar(EasyMDE, easyMDEOpt.toolbar ?? this.easyMDEToolbarDefault);
|
2023-04-03 04:06:57 -06:00
|
|
|
|
|
|
|
this.easyMDE = new EasyMDE(easyMDEOpt);
|
2024-10-22 20:48:04 -06:00
|
|
|
this.easyMDE.codemirror.on('change', () => triggerEditorContentChanged(this.container));
|
2023-04-03 04:06:57 -06:00
|
|
|
this.easyMDE.codemirror.setOption('extraKeys', {
|
|
|
|
'Cmd-Enter': (cm) => handleGlobalEnterQuickSubmit(cm.getTextArea()),
|
|
|
|
'Ctrl-Enter': (cm) => handleGlobalEnterQuickSubmit(cm.getTextArea()),
|
|
|
|
Enter: (cm) => {
|
2024-10-22 20:48:04 -06:00
|
|
|
const tributeContainer = document.querySelector<HTMLElement>('.tribute-container');
|
2023-04-03 04:06:57 -06:00
|
|
|
if (!tributeContainer || tributeContainer.style.display === 'none') {
|
|
|
|
cm.execCommand('newlineAndIndent');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Up: (cm) => {
|
2024-10-22 20:48:04 -06:00
|
|
|
const tributeContainer = document.querySelector<HTMLElement>('.tribute-container');
|
2023-04-03 04:06:57 -06:00
|
|
|
if (!tributeContainer || tributeContainer.style.display === 'none') {
|
|
|
|
return cm.execCommand('goLineUp');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Down: (cm) => {
|
2024-10-22 20:48:04 -06:00
|
|
|
const tributeContainer = document.querySelector<HTMLElement>('.tribute-container');
|
2023-04-03 04:06:57 -06:00
|
|
|
if (!tributeContainer || tributeContainer.style.display === 'none') {
|
|
|
|
return cm.execCommand('goLineDown');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2023-04-07 11:03:29 -06:00
|
|
|
this.applyEditorHeights(this.container.querySelector('.CodeMirror-scroll'), this.options.editorHeights);
|
2023-04-03 04:06:57 -06:00
|
|
|
await attachTribute(this.easyMDE.codemirror.getInputField(), {mentions: true, emoji: true});
|
2024-06-27 23:59:22 -06:00
|
|
|
if (this.dropzone) {
|
|
|
|
initEasyMDEPaste(this.easyMDE, this.dropzone);
|
|
|
|
}
|
2023-04-03 04:06:57 -06:00
|
|
|
hideElem(this.textareaMarkdownToolbar);
|
|
|
|
}
|
|
|
|
|
|
|
|
value(v = undefined) {
|
|
|
|
if (v === undefined) {
|
|
|
|
if (this.easyMDE) {
|
|
|
|
return this.easyMDE.value();
|
|
|
|
}
|
|
|
|
return this.textarea.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.easyMDE) {
|
|
|
|
this.easyMDE.value(v);
|
|
|
|
} else {
|
|
|
|
this.textarea.value = v;
|
|
|
|
}
|
2023-07-30 16:11:15 -06:00
|
|
|
this.textareaAutosize?.resizeToFit();
|
2023-04-03 04:06:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
focus() {
|
|
|
|
if (this.easyMDE) {
|
|
|
|
this.easyMDE.codemirror.focus();
|
|
|
|
} else {
|
|
|
|
this.textarea.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
moveCursorToEnd() {
|
|
|
|
this.textarea.focus();
|
|
|
|
this.textarea.setSelectionRange(this.textarea.value.length, this.textarea.value.length);
|
|
|
|
if (this.easyMDE) {
|
|
|
|
this.easyMDE.codemirror.focus();
|
|
|
|
this.easyMDE.codemirror.setCursor(this.easyMDE.codemirror.lineCount(), 0);
|
|
|
|
}
|
|
|
|
}
|
2023-04-07 11:03:29 -06:00
|
|
|
|
|
|
|
get userPreferredEditor() {
|
|
|
|
return window.localStorage.getItem(`markdown-editor-${this.options.useScene ?? 'default'}`);
|
|
|
|
}
|
|
|
|
set userPreferredEditor(s) {
|
|
|
|
window.localStorage.setItem(`markdown-editor-${this.options.useScene ?? 'default'}`, s);
|
|
|
|
}
|
2023-04-03 04:06:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getComboMarkdownEditor(el) {
|
|
|
|
if (el instanceof $) el = el[0];
|
|
|
|
return el?._giteaComboMarkdownEditor;
|
|
|
|
}
|
|
|
|
|
2024-10-22 20:48:04 -06:00
|
|
|
export async function initComboMarkdownEditor(container: HTMLElement, options = {}) {
|
2023-04-03 04:06:57 -06:00
|
|
|
if (!container) {
|
|
|
|
throw new Error('initComboMarkdownEditor: container is null');
|
|
|
|
}
|
|
|
|
const editor = new ComboMarkdownEditor(container, options);
|
|
|
|
await editor.init();
|
|
|
|
return editor;
|
|
|
|
}
|