From b171b939743de6126f3fb5ca1e2d34bba7cbe428 Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Wed, 2 Nov 2022 12:54:18 -0400
Subject: fix sidebar so active filters add proper metadata to newly created
 documents

---
 src/client/views/SidebarAnnos.tsx | 72 ++++++++++++++++++++++++++-------------
 1 file changed, 48 insertions(+), 24 deletions(-)

(limited to 'src/client/views/SidebarAnnos.tsx')

diff --git a/src/client/views/SidebarAnnos.tsx b/src/client/views/SidebarAnnos.tsx
index 869caabd1..78480da03 100644
--- a/src/client/views/SidebarAnnos.tsx
+++ b/src/client/views/SidebarAnnos.tsx
@@ -1,6 +1,6 @@
 import { computed } from 'mobx';
 import { observer } from 'mobx-react';
-import { Doc, DocListCast, StrListCast } from '../../fields/Doc';
+import { Doc, DocListCast, Field, FieldResult, StrListCast } from '../../fields/Doc';
 import { Id } from '../../fields/FieldSymbols';
 import { List } from '../../fields/List';
 import { DocCast, NumCast, StrCast } from '../../fields/Types';
@@ -39,12 +39,13 @@ export class SidebarAnnos extends React.Component<FieldViewProps & ExtraProps> {
     }
     _stackRef = React.createRef<CollectionStackingView>();
     @computed get allMetadata() {
-        const keys = new Set<string>();
-        DocListCast(this.props.rootDoc[this.sidebarKey]).forEach(doc => SearchBox.documentKeys(doc).forEach(key => keys.add(key)));
-        return Array.from(keys.keys())
-            .filter(key => key[0])
-            .filter(key => key[0] !== '_' && key[0] === key[0].toUpperCase())
-            .sort();
+        const keys = new Map<string, FieldResult<Field>>();
+        DocListCast(this.props.rootDoc[this.sidebarKey]).forEach(doc =>
+            SearchBox.documentKeys(doc)
+                .filter(key => key[0] && key[0] !== '_' && key[0] === key[0].toUpperCase())
+                .map(key => keys.set(key, doc[key]))
+        );
+        return keys;
     }
     @computed get allUsers() {
         const keys = new Set<string>();
@@ -72,26 +73,47 @@ export class SidebarAnnos extends React.Component<FieldViewProps & ExtraProps> {
         });
         FormattedTextBox.SelectOnLoad = target[Id];
         FormattedTextBox.DontSelectInitialText = true;
-        this.allMetadata.map(tag => (target[tag] = tag));
         DocUtils.MakeLink({ doc: anchor }, { doc: target }, 'inline comment:comment on');
 
+        const taggedContent = this.docFilters()
+            .filter(data => data.split(':')[0])
+            .map(data => {
+                const key = data.split(':')[0];
+                const val = Field.Copy(this.allMetadata.get(key));
+                Doc.GetProto(target)[key] = val;
+                return {
+                    type: 'dashField',
+                    attrs: { fieldKey: key, docid: '', hideKey: false, editable: true },
+                    marks: [
+                        { type: 'pFontSize', attrs: { fontSize: '12px' } },
+                        { type: 'pFontFamily', attrs: { family: 'Arial' } },
+                        { type: 'pFontColor', attrs: { color: 'black' } },
+                        { type: 'strong' },
+                        { type: 'user_mark', attrs: { userid: Doc.CurrentUserEmail, modified: 0 } },
+                    ],
+                };
+            });
+        const textLines = [
+            {
+                type: 'paragraph',
+                attrs: { align: null, color: null, id: null, indent: null, inset: null, lineSpacing: null, paddingBottom: null, paddingTop: null },
+                content: [{ type: 'dashField', attrs: { fieldKey: 'text', docid: anchor[Id], hideKey: true, editable: false } }],
+            },
+            { type: 'paragraph', attrs: { align: null, color: null, id: null, indent: null, inset: null, lineSpacing: null, paddingBottom: null, paddingTop: null } },
+        ];
+        const metadatatext = {
+            type: 'paragraph',
+            attrs: { align: null, color: null, id: null, indent: null, inset: null, lineSpacing: null, paddingBottom: null, paddingTop: null },
+            content: taggedContent,
+        };
+        if (taggedContent.length) textLines.push(metadatatext);
         Doc.GetProto(target).text = new RichTextField(
             JSON.stringify({
                 doc: {
                     type: 'doc',
-                    content: [
-                        {
-                            type: 'paragraph',
-                            attrs: { align: null, color: null, id: null, indent: null, inset: null, lineSpacing: null, paddingBottom: null, paddingTop: null },
-                            content: [{ type: 'dashField', attrs: { fieldKey: 'text', docid: anchor[Id], hideKey: true, editable: false } }],
-                        },
-                        {
-                            type: 'paragraph',
-                            attrs: { align: null, color: null, id: null, indent: null, inset: null, lineSpacing: null, paddingBottom: null, paddingTop: null },
-                        },
-                    ],
+                    content: textLines,
                 },
-                selection: { type: 'text', anchor: 4, head: 4 },
+                selection: { type: 'text', anchor: 4, head: 4 }, // set selection to middle paragraph
             }),
             ''
         );
@@ -148,10 +170,10 @@ export class SidebarAnnos extends React.Component<FieldViewProps & ExtraProps> {
                 </div>
             );
         };
-        const renderMeta = (tag: string) => {
-            const active = StrListCast(this.props.rootDoc[this.filtersKey]).includes(`${tag}:${tag}:exists`);
+        const renderMeta = (tag: string, dflt: FieldResult<Field>) => {
+            const active = StrListCast(this.props.rootDoc[this.filtersKey]).includes(`${tag}:${dflt}:exists`);
             return (
-                <div key={tag} className={`sidebarAnnos-filterTag${active ? '-active' : ''}`} onClick={e => Doc.setDocFilter(this.props.rootDoc, tag, tag, 'exists', true, this.sidebarKey, e.shiftKey)}>
+                <div key={tag} className={`sidebarAnnos-filterTag${active ? '-active' : ''}`} onClick={e => Doc.setDocFilter(this.props.rootDoc, tag, dflt, 'exists', true, this.sidebarKey, e.shiftKey)}>
                     {tag}
                 </div>
             );
@@ -178,7 +200,9 @@ export class SidebarAnnos extends React.Component<FieldViewProps & ExtraProps> {
                 }}>
                 <div className="sidebarAnnos-tagList" style={{ height: this.filtersHeight() }} onWheel={e => e.stopPropagation()}>
                     {this.allUsers.map(renderUsers)}
-                    {this.allMetadata.map(renderMeta)}
+                    {Array.from(this.allMetadata.keys())
+                        .sort()
+                        .map(key => renderMeta(key, this.allMetadata.get(key)))}
                 </div>
                 <div style={{ width: '100%', height: `calc(100% - 38px)`, position: 'relative' }}>
                     <CollectionStackingView
-- 
cgit v1.2.3-70-g09d2

