chore(react/type_widget): port notification warning

This commit is contained in:
Elian Doran 2025-09-22 12:07:44 +03:00
parent 78b83cd17b
commit f00f2ee5e4
No known key found for this signature in database
3 changed files with 26 additions and 16 deletions

View File

@ -6,9 +6,10 @@ interface CKEditorWithWatchdogProps extends Pick<HTMLProps<HTMLDivElement>, "cla
isClassicEditor?: boolean; isClassicEditor?: boolean;
watchdogConfig?: WatchdogConfig; watchdogConfig?: WatchdogConfig;
buildEditorOpts: Omit<BuildEditorOptions, "isClassicEditor">; buildEditorOpts: Omit<BuildEditorOptions, "isClassicEditor">;
onNotificationWarning?: (evt: any, data: any) => void;
} }
export default function CKEditorWithWatchdog({ className, tabIndex, isClassicEditor, watchdogConfig, buildEditorOpts }: CKEditorWithWatchdogProps) { export default function CKEditorWithWatchdog({ className, tabIndex, isClassicEditor, watchdogConfig, buildEditorOpts, onNotificationWarning }: CKEditorWithWatchdogProps) {
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => { useEffect(() => {
@ -16,13 +17,21 @@ export default function CKEditorWithWatchdog({ className, tabIndex, isClassicEdi
if (!container) return; if (!container) return;
const watchdog = buildWatchdog(!!isClassicEditor, watchdogConfig); const watchdog = buildWatchdog(!!isClassicEditor, watchdogConfig);
watchdog.setCreator(async () => { watchdog.setCreator(async () => {
const editor = buildEditor(container, !!isClassicEditor, { const editor = await buildEditor(container, !!isClassicEditor, {
...buildEditorOpts, ...buildEditorOpts,
isClassicEditor: !!isClassicEditor isClassicEditor: !!isClassicEditor
}); });
if (onNotificationWarning) {
editor.plugins.get("Notification").on("show:warning", onNotificationWarning);
}
return editor; return editor;
}); });
watchdog.create(container); watchdog.create(container);
return () => watchdog.destroy();
}, []); }, []);
return ( return (

View File

@ -1,3 +1,4 @@
import toast from "../../../services/toast";
import { isMobile } from "../../../services/utils"; import { isMobile } from "../../../services/utils";
import { useNoteLabel, useTriliumOption } from "../../react/hooks"; import { useNoteLabel, useTriliumOption } from "../../react/hooks";
import { TypeWidgetProps } from "../type_widget"; import { TypeWidgetProps } from "../type_widget";
@ -32,7 +33,21 @@ export default function EditableText({ note }: TypeWidgetProps) {
contentLanguage: language ?? null, contentLanguage: language ?? null,
forceGplLicense: false, forceGplLicense: false,
}} }}
onNotificationWarning={onNotificationWarning}
/> />
</div> </div>
) )
} }
function onNotificationWarning(data, evt) {
const title = data.title;
const message = data.message.message;
if (title && message) {
toast.showErrorTitleAndMessage(data.title, data.message.message);
} else if (title) {
toast.showError(title || message);
}
evt.stop();
}

View File

@ -57,20 +57,6 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
this.watchdog.setCreator(async (_, editorConfig) => { this.watchdog.setCreator(async (_, editorConfig) => {
logInfo("Creating new CKEditor"); logInfo("Creating new CKEditor");
const notificationsPlugin = editor.plugins.get("Notification");
notificationsPlugin.on("show:warning", (evt, data) => {
const title = data.title;
const message = data.message.message;
if (title && message) {
toast.showErrorTitleAndMessage(data.title, data.message.message);
} else if (title) {
toast.showError(title || message);
}
evt.stop();
});
if (isClassicEditor) { if (isClassicEditor) {
const $classicToolbarWidget = this.findClassicToolbar(); const $classicToolbarWidget = this.findClassicToolbar();