Skip to content

Commit b070f5b

Browse files
committed
Add: patch for TinyMCE to format wc-mermaid properly
1 parent adf8552 commit b070f5b

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

builder/src/main.rs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -258,32 +258,41 @@ fn search_and_replace_file<
258258
//
259259
// These functions simplify common build-focused development tasks and support
260260
// CI builds.
261-
/// After updating files in the client's Node files, perform some fix-ups.
262-
fn patch_client_npm() -> io::Result<()> {
263-
// Apply a the fixes described in
264-
// [issue 27](https://github.com/bjones1/CodeChat_Editor/issues/27).
265-
//
266-
// Insert this line...
267-
let patch = "
268-
selectionNotFocus = this.view.state.facet(editable) ? focused : hasSelection(this.dom, this.view.observer.selectionRange)";
269-
// After this line.
270-
let before_path = " let selectionNotFocus = !focused && !(this.view.state.facet(editable) || this.dom.tabIndex > -1) &&
271-
hasSelection(this.dom, this.view.observer.selectionRange) && !(activeElt && this.dom.contains(activeElt));";
272-
// First, see if the patch was applied already.
273-
let index_js_path = Path::new("../client/node_modules/@codemirror/view/dist/index.js");
274-
let index_js = fs::read_to_string(index_js_path)?;
275-
if !index_js.contains(patch) {
276-
let patch_loc = index_js
277-
.find(before_path)
261+
/// Apply the provided patch to a file.
262+
fn patch_file(patch: &str, before_patch: &str, file_path: &str) -> io::Result<()> {
263+
let file_path = Path::new(file_path);
264+
let file_contents = fs::read_to_string(file_path)?;
265+
if !file_contents.contains(patch) {
266+
let patch_loc = file_contents
267+
.find(before_patch)
278268
.expect("Patch location not found.")
279-
+ before_path.len();
280-
let patched_index_js = format!(
269+
+ before_patch.len();
270+
let patched_file_contents = format!(
281271
"{}{patch}{}",
282-
&index_js[..patch_loc],
283-
&index_js[patch_loc..]
272+
&file_contents[..patch_loc],
273+
&file_contents[patch_loc..]
284274
);
285-
fs::write(index_js_path, &patched_index_js)?;
275+
fs::write(file_path, &patched_file_contents)?;
286276
}
277+
Ok(())
278+
}
279+
/// After updating files in the client's Node files, perform some fix-ups.
280+
fn patch_client_npm() -> io::Result<()> {
281+
// Apply a the fixes described in
282+
// [issue 27](https://github.com/bjones1/CodeChat_Editor/issues/27).
283+
patch_file(
284+
"
285+
selectionNotFocus = this.view.state.facet(editable) ? focused : hasSelection(this.dom, this.view.observer.selectionRange)",
286+
" let selectionNotFocus = !focused && !(this.view.state.facet(editable) || this.dom.tabIndex > -1) &&
287+
hasSelection(this.dom, this.view.observer.selectionRange) && !(activeElt && this.dom.contains(activeElt));",
288+
"../client/node_modules/@codemirror/view/dist/index.js"
289+
)?;
290+
// In [older releases](https://www.tiny.cloud/docs/tinymce/5/6.0-upcoming-changes/#options), TinyMCE allowed users to change `whitespace_elements`; the whitespace inside these isn't removed by TinyMCE. However, this was removed in v6.0. Therefore, manually patch TinyMCE instead.
291+
patch_file(
292+
" wc-mermaid",
293+
"const whitespaceElementsMap = createLookupTable('whitespace_elements', 'pre script noscript style textarea video audio iframe object code",
294+
"../client/node_modules/tinymce/tinymce.js"
295+
)?;
287296

288297
// Copy across the parts of MathJax that are needed, since bundling it is
289298
// difficult.

0 commit comments

Comments
 (0)