Skip to content
Merged

Dev #60

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions builder/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

291 changes: 172 additions & 119 deletions client/package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codechat-editor-client",
"version": "0.1.11",
"version": "0.1.12",
"description": "The CodeChat Editor Client, part of a web-based literate programming editor (the CodeChat Editor).",
"homepage": "https://github.com/bjones1/CodeChat_Editor",
"type": "module",
Expand Down Expand Up @@ -51,6 +51,7 @@
"mathjax": "^4.0.0-beta.7",
"mathjax-modern-font": "4.0.0-beta.7",
"mermaid": "^11",
"npm-check-updates": "^17.1.15",
"tinymce": "^7"
},
"repository": {
Expand Down
27 changes: 20 additions & 7 deletions client/src/CodeChatEditorFramework.mts
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,11 @@ class WebSocketComm {
// completed before updating the editable contents.
if (this.onloading) {
root_iframe!.onload = () => {
root_iframe!.contentWindow!.CodeChatEditor.open_lp(
contents,
);
set_content(contents);
this.onloading = false;
};
} else {
root_iframe!.contentWindow!.CodeChatEditor.open_lp(
contents,
);
set_content(contents);
}
} else {
// TODO: handle scroll/cursor updates.
Expand All @@ -167,7 +163,7 @@ class WebSocketComm {
const current_file = value as string;
// If the page is still loading, then don't save. Otherwise,
// save the editor contents if necessary.
let cce = root_iframe?.contentWindow?.CodeChatEditor;
let cce = get_client();
let promise =
cce !== undefined
? cce.on_save(true)
Expand Down Expand Up @@ -305,6 +301,23 @@ class WebSocketComm {
};
}

// Return the `CodeChatEditor` object if the `root_iframe` contains the Client; otherwise, this is `undefined`.
const get_client = () => root_iframe?.contentWindow?.CodeChatEditor;

// Assign content to either the Client (if it's loaded) or the webpage (if not) in the `root_iframe`.
const set_content = (contents: CodeChatForWeb) => {
let client = get_client();
if (client === undefined) {
let cw = root_iframe!.contentWindow!;
cw.document.open();
cw.document.write(contents.source.doc);
cw.document.close();
} else {
root_iframe!.contentWindow!.CodeChatEditor.open_lp(
contents);
}
};

// The iframe element which composes this page.
let root_iframe: HTMLIFrameElement | undefined;

Expand Down
30 changes: 27 additions & 3 deletions client/src/CodeMirror-integration.mts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ import { set_is_dirty, startAutosaveTimer } from "./CodeChatEditor.mjs";
// -------
let current_view: EditorView;
let tinymce_singleton: Editor | undefined;
// When true, don't update on the next call to `on_dirty`. See that function for
// more info.
let ignore_next_dirty = false;

declare global {
Expand Down Expand Up @@ -437,7 +439,23 @@ const element_is_in_doc_block = (target: EventTarget | null): boolean | HTMLDivE
return false;
};

// Called when a doc block is dirty.
// Called when a doc block is dirty...
//
// ...but it's more complicated than that. TinyMCE keeps track of a [dirty
// flag](https://www.tiny.cloud/docs/tinymce/latest/apis/tinymce.editor/#isDirty),
// but some dirty events it reports shouldn't be saved:
//
// 1. When the existing TinyMCE instance is updated with new text on a redraw,
// the resulting dirty flag should be ignored.
// 2. When the existing TinyMCE instance is focused, existing math should be
// untypeset, then the dirty ignored.
// 3. When MathJax typesets math on a TinyMCE focus out event, the dirty flag
// gets set. This should be ignored. However, typesetting is an async
// operation, so we assume it's OK to await the typeset completion, then
// clear the `ignore_next_dirty flag`. This will lead to nasty bugs at some
// point.
// 4. When an HTML doc block is assigned to the TinyMCE instance for editing,
// the dirty flag is set. This must be ignored.
const on_dirty = (
// The div that's dirty. It must be a child of the doc block div.
event_target: HTMLElement,
Expand Down Expand Up @@ -523,6 +541,9 @@ const DocBlockPlugin = ViewPlugin.fromClass(
if (is_tinymce) {
ignore_next_dirty = true;
mathJaxUnTypeset(contents_div);
// If there was no math to untypeset, then `on_dirty` wasn't
// called, but we should no longer ignore the next dirty
// flag.
ignore_next_dirty = false;
} else {
// Wait until the focus event completes; this causes the
Expand Down Expand Up @@ -858,14 +879,17 @@ export const CodeMirror_load = async (
if (target_or_false == null) {
return false;
}
if (!tinymce_singleton!.isDirty()) {
ignore_next_dirty = true;
// If the editor is dirty, save it first before we possibly
// modify it.
if (tinymce_singleton!.isDirty()) {
tinymce_singleton!.save();
}
// When switching from one doc block to another, the MathJax
// typeset finishes after the new doc block has been
// updated. To prevent saving the "dirty" content from
// typesetting, wait until this finishes to clear the
// `ignore_next_dirty` flag.
ignore_next_dirty = true;
mathJaxTypeset(target_or_false, () => {
tinymce_singleton!.save();
ignore_next_dirty = false;
Expand Down
5 changes: 4 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ Changelog

* [Github master](https://github.com/bjones1/CodeChat_Editor):
* No changes.
* v0.1.12, 2025-Mar-08:
* Fixed error when creating a new document in VSCode.
* Fixed error when updating a non-CodeChat Editor document in VSCode.
* v0.1.11, 2025-Feb-27:
* Fixed data corruption while editing math: typeset math, instead of LaTeX
source, was saved to the source file. Now, math is untypeset during
edits, then retypeset afterwards.
* Correctly handle webview shutdown in VSCode extension.
* v0.1.10, 2025-Feb-20:
* Update to the 2024 editing of Rust.
* Update to the 2024 edition of Rust.
* Update dependencies.
* Update source formatting using current CodeChat Editor.
* v0.1.9, 2025-Jan-20:
Expand Down
Loading
Loading