2023-08-04 23:50:54 +08:00

45 lines
1.5 KiB
TypeScript

import {RangeStatic, Quill, DeltaStatic} from "quill";
import {EditorContent, EditorFormat} from "../../types/editor.ts";
export const EDITOR: {
instance: Quill | null,
setContents: (content: EditorContent) => void;
trigger: (eventType: string, data: any) => void;
on(type: string, handler: (data: any) => void): void;
} = {
instance: null,
setContents: (content) => {
EDITOR.instance?.setContents(content as DeltaStatic)
},
trigger: (eventType, data) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
EDITOR.instance?.emitter.emit(eventType, data);
},
on: (type, handler) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
EDITOR.instance?.on(type, handler)
}
}
// const FormatLines = ['align']
export function initEditorProcessor(editor: Quill) {
return {
// 格式化
format(range: RangeStatic, format: keyof EditorFormat, value: string | number | boolean) {
const {index, length} = range
if (format == 'align') {
editor.formatLine(index, length, format, value);
return;
}
editor.formatText(range, format, value);
},
selectNode(node: HTMLElement) {
const blot = Quill.find(node)
const offset = blot.offset(blot.scroll), length = blot.length()
editor.setSelection(offset, length)
}
}
}