From 0d8c05db9cbb5a23a94554c65015c347ff8c38b9 Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Wed, 31 Jan 2024 20:39:47 -0500
Subject: cleaned up accessing/setting proto_embeddings with api on Doc.  fixed
 some css related to IconButtons.  added a paintView toggle button to dec
 decorations for text with code blocks. enabled text with code to modify Docs
 and get this and documentView as params.

---
 .../views/nodes/formattedText/FormattedTextBox.tsx | 55 +++++++++++++---------
 1 file changed, 32 insertions(+), 23 deletions(-)

(limited to 'src/client/views/nodes/formattedText/FormattedTextBox.tsx')

diff --git a/src/client/views/nodes/formattedText/FormattedTextBox.tsx b/src/client/views/nodes/formattedText/FormattedTextBox.tsx
index 973f90501..b82ab4219 100644
--- a/src/client/views/nodes/formattedText/FormattedTextBox.tsx
+++ b/src/client/views/nodes/formattedText/FormattedTextBox.tsx
@@ -67,8 +67,6 @@ import { RichTextMenu, RichTextMenuPlugin } from './RichTextMenu';
 import { RichTextRules } from './RichTextRules';
 import { schema } from './schema_rts';
 import { SummaryView } from './SummaryView';
-import { CollectionView } from '../../collections/CollectionView';
-import { PaintButtonView } from './PaintButtonView';
 // import * as applyDevTools from 'prosemirror-dev-tools';
 @observer
 export class FormattedTextBox extends ViewBoxAnnotatableComponent<FieldViewProps>() implements ViewBoxInterface {
@@ -488,14 +486,29 @@ export class FormattedTextBox extends ViewBoxAnnotatableComponent<FieldViewProps
     };
 
     // creates links between terms in a document and published documents (myPublishedDocs) that have titles starting with an '@'
+    /**
+     * Searches the text for occurences of any strings that match the names of 'published' documents.  These document
+     * names will begin with an '@' prefix.  However, valid matches within the text can have any of the following formats:
+     *    name, @<name>, or ^@<name>
+     * The last of these is interpreted as an include directive when converting the text into evaluated code in the paint
+     * function of a freeform view that is driven by the text box's text.  The include directive will copy the code of the published
+     * document into the code being evaluated.
+     */
     hyperlinkTerm = (tr: any, target: Doc, newAutoLinks: Set<Doc>) => {
         const editorView = this._editorView;
         if (editorView && (editorView as any).docView && !Doc.AreProtosEqual(target, this.Document)) {
             const autoLinkTerm = StrCast(target.title).replace(/^@/, '');
             var alink: Doc | undefined;
             this.findInNode(editorView, editorView.state.doc, autoLinkTerm).forEach(sel => {
-                const splitter = editorView.state.schema.marks.splitter.create({ id: Utils.GenerateGuid() });
-                if (!sel.$anchor.pos || [autoLinkTerm, StrCast(target.title)].includes(editorView.state.doc.textBetween(sel.$anchor.pos - 1, sel.$to.pos).trim())) {
+                if (
+                    !sel.$anchor.pos ||
+                    autoLinkTerm ===
+                        editorView.state.doc
+                            .textBetween(sel.$anchor.pos - 1, sel.$to.pos)
+                            .trim()
+                            .replace(/[\^@]+/, '')
+                ) {
+                    const splitter = editorView.state.schema.marks.splitter.create({ id: Utils.GenerateGuid() });
                     tr = tr.addMark(sel.from, sel.to, splitter);
                     tr.doc.nodesBetween(sel.from, sel.to, (node: any, pos: number, parent: any) => {
                         if (node.firstChild === null && !node.marks.find((m: Mark) => m.type.name === schema.marks.noAutoLinkAnchor.name) && node.marks.find((m: Mark) => m.type.name === schema.marks.splitter.name)) {
@@ -668,12 +681,22 @@ export class FormattedTextBox extends ViewBoxAnnotatableComponent<FieldViewProps
             let index = 0,
                 foundAt;
             const ep = this.getNodeEndpoints(pm.state.doc, node);
-            const regexp = new RegExp(find.replace('*', ''), 'i');
+            const regexp = new RegExp(find, 'i');
             if (regexp) {
-                while (ep && (foundAt = node.textContent.slice(index).search(regexp)) > -1) {
-                    const sel = new TextSelection(pm.state.doc.resolve(ep.from + index + foundAt + 1), pm.state.doc.resolve(ep.from + index + foundAt + find.length + 1));
-                    ret.push(sel);
-                    index = index + foundAt + find.length;
+                var blockOffset = 0;
+                for (var i = 0; i < node.childCount; i++) {
+                    var textContent = '';
+                    while (i < node.childCount && node.child(i).type === pm.state.schema.nodes.text) {
+                        textContent += node.child(i).textContent;
+                        i++;
+                    }
+                    while (ep && (foundAt = textContent.slice(index).search(regexp)) > -1) {
+                        const sel = new TextSelection(pm.state.doc.resolve(ep.from + index + blockOffset + foundAt + 1), pm.state.doc.resolve(ep.from + index + blockOffset + foundAt + find.length + 1));
+                        ret.push(sel);
+                        index = index + foundAt + find.length;
+                    }
+                    blockOffset += textContent.length;
+                    if (i < node.childCount) blockOffset += node.child(i).nodeSize;
                 }
             }
         } else {
@@ -934,17 +957,6 @@ export class FormattedTextBox extends ViewBoxAnnotatableComponent<FieldViewProps
                 event: () => (this.layoutDoc._createDocOnCR = !this.layoutDoc._createDocOnCR),
                 icon: !this.Document._createDocOnCR ? 'grip-lines' : 'bars',
             });
-        optionItems.push({
-            description: 'Make Paint Function',
-            event: () => {
-                this.dataDoc.layout_painted = CollectionView.LayoutString('painted');
-                this.layoutDoc.layout_fieldKey = 'layout_painted';
-                this.layoutDoc.type_collection = CollectionViewType.Freeform;
-                this.DocumentView?.().setToggleDetail();
-                this.dataDoc.paintFunc = ComputedField.MakeFunction(`toJavascriptString(this['${this.fieldKey}']?.Text)`);
-            },
-            icon: !this.Document._layout_enableAltContentUI ? 'eye-slash' : 'eye',
-        });
         !Doc.noviceMode &&
             optionItems.push({
                 description: `${this.Document._layout_autoHeight ? 'Lock' : 'Auto'} Height`,
@@ -1411,9 +1423,6 @@ export class FormattedTextBox extends ViewBoxAnnotatableComponent<FieldViewProps
                     dashField(node: any, view: any, getPos: any) {
                         return new DashFieldView(node, view, getPos, self);
                     },
-                    paintButton(node: any, view: any, getPos: any) {
-                        return new PaintButtonView(node, view, getPos, self);
-                    },
                     equation(node: any, view: any, getPos: any) {
                         return new EquationView(node, view, getPos, self);
                     },
-- 
cgit v1.2.3-70-g09d2

