chore(ckeditor): create shared type for editor

This commit is contained in:
Elian Doran 2025-05-05 21:44:27 +03:00
parent 6386c45212
commit 70ec38d534
No known key found for this signature in database
4 changed files with 23 additions and 8 deletions

View File

@ -26,7 +26,7 @@ import type TypeWidget from "../widgets/type_widgets/type_widget.js";
import type EditableTextTypeWidget from "../widgets/type_widgets/editable_text.js"; import type EditableTextTypeWidget from "../widgets/type_widgets/editable_text.js";
import type { NativeImage, TouchBar } from "electron"; import type { NativeImage, TouchBar } from "electron";
import TouchBarComponent from "./touch_bar.js"; import TouchBarComponent from "./touch_bar.js";
import type { ClassicEditor, PopupEditor } from "@triliumnext/ckeditor5"; import type { CKTextEditor } from "@triliumnext/ckeditor5";
interface Layout { interface Layout {
getRootWidget: (appContext: AppContext) => RootWidget; getRootWidget: (appContext: AppContext) => RootWidget;
@ -188,7 +188,7 @@ export type CommandMappings = {
callback: (value: NoteDetailWidget | PromiseLike<NoteDetailWidget>) => void; callback: (value: NoteDetailWidget | PromiseLike<NoteDetailWidget>) => void;
}; };
executeWithTextEditor: CommandData & executeWithTextEditor: CommandData &
ExecuteCommandData<ClassicEditor | PopupEditor> & { ExecuteCommandData<CKTextEditor> & {
callback?: GetTextEditorCallback; callback?: GetTextEditorCallback;
}; };
executeWithCodeEditor: CommandData & ExecuteCommandData<CodeMirrorInstance>; executeWithCodeEditor: CommandData & ExecuteCommandData<CodeMirrorInstance>;

View File

@ -10,14 +10,14 @@ import options from "../services/options.js";
import type { ViewScope } from "../services/link.js"; import type { ViewScope } from "../services/link.js";
import type FNote from "../entities/fnote.js"; import type FNote from "../entities/fnote.js";
import type TypeWidget from "../widgets/type_widgets/type_widget.js"; import type TypeWidget from "../widgets/type_widgets/type_widget.js";
import type { ClassicEditor, PopupEditor } from "@triliumnext/ckeditor5"; import type { CKTextEditor } from "@triliumnext/ckeditor5";
export interface SetNoteOpts { export interface SetNoteOpts {
triggerSwitchEvent?: unknown; triggerSwitchEvent?: unknown;
viewScope?: ViewScope; viewScope?: ViewScope;
} }
export type GetTextEditorCallback = (editor: ClassicEditor | PopupEditor) => void; export type GetTextEditorCallback = (editor: CKTextEditor) => void;
class NoteContext extends Component implements EventListener<"entitiesReloaded"> { class NoteContext extends Component implements EventListener<"entitiesReloaded"> {
ntxId: string | null; ntxId: string | null;
@ -299,7 +299,7 @@ class NoteContext extends Component implements EventListener<"entitiesReloaded">
} }
async getTextEditor(callback?: GetTextEditorCallback) { async getTextEditor(callback?: GetTextEditorCallback) {
return this.timeout<ClassicEditor | PopupEditor>( return this.timeout<CKTextEditor>(
new Promise((resolve) => new Promise((resolve) =>
appContext.triggerCommand("executeWithTextEditor", { appContext.triggerCommand("executeWithTextEditor", {
callback, callback,

View File

@ -18,7 +18,7 @@ import { buildSelectedBackgroundColor } from "../../components/touch_bar.js";
import { buildConfig, buildToolbarConfig } from "./ckeditor/config.js"; import { buildConfig, buildToolbarConfig } from "./ckeditor/config.js";
import type FNote from "../../entities/fnote.js"; import type FNote from "../../entities/fnote.js";
import { getMermaidConfig } from "../../services/mermaid.js"; import { getMermaidConfig } from "../../services/mermaid.js";
import { PopupEditor, ClassicEditor, EditorWatchdog } from "@triliumnext/ckeditor5"; import { PopupEditor, ClassicEditor, EditorWatchdog, type CKTextEditor } from "@triliumnext/ckeditor5";
const ENABLE_INSPECTOR = false; const ENABLE_INSPECTOR = false;
@ -128,7 +128,7 @@ function buildListOfLanguages() {
export default class EditableTextTypeWidget extends AbstractTextTypeWidget { export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
private contentLanguage?: string | null; private contentLanguage?: string | null;
private watchdog!: EditorWatchdog<ClassicEditor | PopupEditor>; private watchdog!: EditorWatchdog<CKTextEditor>;
private $editor!: JQuery<HTMLElement>; private $editor!: JQuery<HTMLElement>;
@ -158,7 +158,7 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
// display of $widget in both branches. // display of $widget in both branches.
this.$widget.show(); this.$widget.show();
this.watchdog = new EditorWatchdog<ClassicEditor | PopupEditor>(editorClass, { this.watchdog = new EditorWatchdog<CKTextEditor>(editorClass, {
// An average number of milliseconds between the last editor errors (defaults to 5000). // An average number of milliseconds between the last editor errors (defaults to 5000).
// When the period of time between errors is lower than that and the crashNumberLimit // When the period of time between errors is lower than that and the crashNumberLimit
// is also reached, the watchdog changes its state to crashedPermanently, and it stops // is also reached, the watchdog changes its state to crashedPermanently, and it stops

View File

@ -3,18 +3,33 @@ import { COMMON_PLUGINS, POPUP_EDITOR_PLUGINS } from "./plugins";
import { BalloonEditor, DecoupledEditor } from "ckeditor5"; import { BalloonEditor, DecoupledEditor } from "ckeditor5";
export { EditorWatchdog } from "ckeditor5"; export { EditorWatchdog } from "ckeditor5";
/**
* Short-hand for the CKEditor classes supported by Trilium for text editing.
* Specialized editors such as the {@link AttributeEditor} are not included.
*/
export type CKTextEditor = ClassicEditor | PopupEditor;
/**
* The text editor that can be used for editing attributes and relations.
*/
export class AttributeEditor extends BalloonEditor { export class AttributeEditor extends BalloonEditor {
static override get builtinPlugins() { static override get builtinPlugins() {
return []; return [];
} }
} }
/**
* A text editor configured as a {@link DecoupledEditor} (fixed toolbar mode), as well as its preconfigured plugins.
*/
export class ClassicEditor extends DecoupledEditor { export class ClassicEditor extends DecoupledEditor {
static override get builtinPlugins() { static override get builtinPlugins() {
return COMMON_PLUGINS; return COMMON_PLUGINS;
} }
} }
/**
* A text editor configured as a {@link BalloonEditor} (floating toolbar mode), as well as its preconfigured plugins.
*/
export class PopupEditor extends BalloonEditor { export class PopupEditor extends BalloonEditor {
static override get builtinPlugins() { static override get builtinPlugins() {
return POPUP_EDITOR_PLUGINS; return POPUP_EDITOR_PLUGINS;