mirror of
https://github.com/go-gitea/gitea.git
synced 2026-12-04 15:54:57 +00:00
refactor: use data-global-init for ComboMarkdownEditor (#40479)
## TL;DR: For custom-init markdown editors, do nothing. For non-custom-init markdown editors, change from an imperative scan to a declarative setup. ## Longer version As part of the `data-global-init` migration effort, this moves ComboMarkdownEditor's automatic setup onto `data-global-init`. ### Before `initGlobalComboMarkdownEditor()` ran a one-off startup sweep of its own: `queryElems(document, '.combo-markdown-editor:not(.custom-init)', ...)`. The editor template relied on that page-wide selector, with the `custom-init` class as an exclusion, rather than declaring its own setup. ### After - The editor template marks its root element with `data-global-init="initComboMarkdownEditor"`, and the sweep is replaced by a registered handler, like other migrated components. - The attribute goes only on editors without `CustomInit`. `CustomInit` editors are unchanged: they get no attribute, and their page code sets them up as before. - Setup now goes through the shared observer, so the editor follows the same rules as every other `data-global-init` component, including elements added after page load. ### Why - The template now names its own setup: `data-global-init="initComboMarkdownEditor"` leads straight to the registered handler. Before, nothing in the template said how the editor was set up, and the `custom-init` class only meant something because of a selector in `common-form.ts`. - It follows the guidance in `modules/observer.ts`, which recommends `registerGlobalInitFunc` over selector-based setup as "less efficient and less maintainable", and removes one of the `queryElems(document, ...)` sweeps that `utils/dom.ts` marks for refactoring. - The editor no longer needs its own page-wide query at startup: it's picked up by the observer's single initial pass, like the other `data-global-init` components (55 templates use it today). ### Notes - Editors are now set up by the observer's initial sweep, which runs after the page-specific init code rather than before it. Every direct editor lookup (`getComboMarkdownEditor` in `repo-release.ts`, `repo-issue-edit.ts`, `repo-issue.ts` and the monospace toggle in `ComboMarkdownEditor.ts`) runs on a user action, so none is affected. - This is the first step toward removing `CustomInit` entirely. That also needs JS-only options moved into presets and setup made idempotent, which are separate changes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
4 files changed
+10
-13
No files matched your search
@@ -1,5 +1,5 @@
|
||||
{{/* Template Attributes:
|
||||
* CustomInit: do not initialize the editor automatically
|
||||
* CustomInit: do not add data-global-init, the caller initializes the editor itself
|
||||
* ContainerId: id attribute for the container element
|
||||
* ContainerClasses: additional classes for the container element
|
||||
* MarkdownEditorContext: the context data for the editor, see backend MarkdownEditorContext
|
||||
@@ -19,7 +19,8 @@
|
||||
{{$mentionsLink := or $editorContext.MentionsLink ""}}
|
||||
{{/* don't try to remove it, there are still many users who like it: https://github.com/go-gitea/gitea/issues/38228#issuecomment-4809312561 */}}
|
||||
{{$supportEasyMDE := or (eq $previewMode "comment") (eq $previewMode "wiki")}}
|
||||
<div {{if .ContainerId}}id="{{.ContainerId}}"{{end}} class="combo-markdown-editor {{if .CustomInit}}custom-init{{end}} {{.ContainerClasses}}"
|
||||
<div {{if .ContainerId}}id="{{.ContainerId}}"{{end}} class="combo-markdown-editor {{.ContainerClasses}}"
|
||||
{{if not .CustomInit}}data-global-init="initComboMarkdownEditor"{{end}}
|
||||
data-dropzone-parent-container="{{.DropzoneParentContainer}}"
|
||||
data-content-mode="{{$previewMode}}"
|
||||
data-support-easy-mde="{{$supportEasyMDE}}"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {handleGlobalEnterQuickSubmit} from './comp/QuickSubmit.ts';
|
||||
import {queryElems} from '../utils/dom.ts';
|
||||
import {initComboMarkdownEditor} from './comp/ComboMarkdownEditor.ts';
|
||||
import {registerGlobalInitFunc} from '../modules/observer.ts';
|
||||
|
||||
export function initGlobalEnterQuickSubmit() {
|
||||
document.addEventListener('keydown', (e) => {
|
||||
@@ -18,5 +18,5 @@ export function initGlobalEnterQuickSubmit() {
|
||||
}
|
||||
|
||||
export function initGlobalComboMarkdownEditor() {
|
||||
queryElems<HTMLElement>(document, '.combo-markdown-editor:not(.custom-init)', (el) => initComboMarkdownEditor(el));
|
||||
registerGlobalInitFunc('initComboMarkdownEditor', (el) => initComboMarkdownEditor(el));
|
||||
}
|
||||
@@ -26,11 +26,7 @@ import type EasyMDE from 'easymde';
|
||||
import type Dropzone from '@deltablot/dropzone';
|
||||
import {localUserSettings} from '../../modules/user-settings.ts';
|
||||
|
||||
/**
|
||||
* validate if the given textarea is non-empty.
|
||||
* @param {HTMLTextAreaElement} textarea - The textarea element to be validated.
|
||||
* @returns {boolean} returns true if validation succeeded.
|
||||
*/
|
||||
/** validate if the given textarea is non-empty. */
|
||||
export function validateTextareaNonEmpty(textarea: HTMLTextAreaElement) {
|
||||
// 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.
|
||||
|
||||
@@ -11,8 +11,8 @@ const selectorHandlers: SelectorHandler<Element>[] = [];
|
||||
type GlobalEventFunc<T extends HTMLElement, E extends Event> = (el: T, e: E) => Promisable<void>;
|
||||
const globalEventFuncs: Record<string, GlobalEventFunc<HTMLElement, Event>> = {};
|
||||
|
||||
type GlobalInitFunc<T extends Element> = (el: T) => Promisable<void>;
|
||||
const globalInitFuncs: Record<string, GlobalInitFunc<Element>> = {};
|
||||
type GlobalInitFunc<T extends Element, R> = (el: T) => Promisable<R>;
|
||||
const globalInitFuncs: Record<string, GlobalInitFunc<Element, unknown>> = {};
|
||||
|
||||
// It handles the global events for all `<div data-global-click="onSomeElemClick"></div>` elements.
|
||||
export function registerGlobalEventFunc<T extends HTMLElement, E extends Event>(event: string, name: string, func: GlobalEventFunc<T, E>) {
|
||||
@@ -35,8 +35,8 @@ export function registerGlobalSelectorFunc<T extends Element>(selector: string,
|
||||
}
|
||||
|
||||
// It handles the global init functions for all `<div data-global-init="initSomeElem"></div>` elements.
|
||||
export function registerGlobalInitFunc<T extends HTMLElement>(name: string, handler: GlobalInitFunc<T>) {
|
||||
globalInitFuncs[name] = handler as GlobalInitFunc<Element>;
|
||||
export function registerGlobalInitFunc<T extends HTMLElement, R>(name: string, handler: GlobalInitFunc<T, R>) {
|
||||
globalInitFuncs[name] = handler as GlobalInitFunc<Element, unknown>;
|
||||
// The "global init" functions are managed internally and called by callGlobalInitFunc
|
||||
// They must be ready before initGlobalSelectorObserver is called.
|
||||
if (globalSelectorObserverInited) throw new Error('registerGlobalInitFunc() must be called before initGlobalSelectorObserver()');
|
||||
|
||||
Reference in new issue
Block a user