Skip to content

Commit 4b5c7f4

Browse files
committed
Fix: correctly access TinyMCE instance inside dirty handler.
Clean up type of the global tinymce object.
1 parent 39e9b34 commit 4b5c7f4

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

client/src/CodeChatEditor.mts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ import "./graphviz-webcomponent-setup.mts";
6262
// This must be imported *after* the previous setup import, so it's placed here,
6363
// instead of in the third-party category above.
6464
import "./third-party/graphviz-webcomponent/graph.js";
65-
import { tinymce, init, Editor } from "./tinymce-config.mjs";
65+
import { init, tinymce } from "./tinymce-config.mjs";
66+
import { Editor, EditorEvent, Events } from "tinymce";
6667
import {
6768
CodeChatForWeb,
6869
CodeMirrorDiffable,
@@ -247,11 +248,17 @@ const _open_lp = async (
247248
// [handling editor events](https://www.tiny.cloud/docs/tinymce/6/events/#handling-editor-events),
248249
// this is how to create a TinyMCE event handler.
249250
setup: (editor: Editor) => {
250-
editor.on("dirty", () => {
251-
tinymce.activeEditor!.setDirty(false);
252-
is_dirty = true;
253-
startAutosaveTimer();
254-
});
251+
editor.on(
252+
"dirty",
253+
(
254+
event: EditorEvent<Events.EditorEventMap["dirty"]>,
255+
) => {
256+
// Sometimes, `tinymce.activeEditor` is null (perhaps when it's not focused). Use the `event` data instead.
257+
event.target.setDirty(false);
258+
is_dirty = true;
259+
startAutosaveTimer();
260+
},
261+
);
255262
},
256263
});
257264
tinymce.activeEditor!.focus();

client/src/CodeMirror-integration.mts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ import { python } from "@codemirror/lang-python";
8282
import { rust } from "@codemirror/lang-rust";
8383
import { sql } from "@codemirror/lang-sql";
8484
import { yaml } from "@codemirror/lang-yaml";
85-
import { Editor, init, tinymce } from "./tinymce-config.mjs";
86-
import { EditorEvent, Events } from "tinymce";
85+
import { tinymce, init } from "./tinymce-config.mjs";
86+
import { Editor, EditorEvent, Events } from "tinymce";
8787

8888
// ### Local
8989
import {
@@ -1035,10 +1035,10 @@ export const CodeMirror_load = async (
10351035
editor.on(
10361036
"Dirty",
10371037
(event: EditorEvent<Events.EditorEventMap["dirty"]>) => {
1038-
// Get the div TinyMCE stores edits in. TODO: find
1039-
// documentation for `event.target.bodyElement`.
1040-
tinymce.activeEditor!.setDirty(false);
1041-
const target_or_false = event.target?.bodyElement;
1038+
// Sometimes, `tinymce.activeEditor` is null (perhaps when it's not focused). Use the `event` data instead.
1039+
event.target.setDirty(false);
1040+
// Get the div TinyMCE stores edits in.
1041+
const target_or_false = event.target.bodyElement;
10421042
if (target_or_false == null) {
10431043
return;
10441044
}

client/src/tinymce-config.mts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import {
2525
RawEditorOptions,
2626
TinyMCE,
2727
} from "tinymce";
28-
// TODO: The type of tinymce is broken; I don't know why. Here's a workaround.
28+
// This is taken from the [TinyMCE example code](https://github.com/tinymce/tinymce/blob/main/modules/tinymce/src/core/demo/ts/demo/TinyMceDemo.ts). However, esbuild doesn't accept it.
29+
//export declare const tinymce: TinyMCE;
30+
// Here's a workaround.
2931
export const tinymce = tinymce_ as unknown as TinyMCE;
30-
export { Editor };
3132

3233
// Default icons are required for TinyMCE 5.3 or above.
3334
import "tinymce/icons/default/index.js";

0 commit comments

Comments
 (0)