From 431a03690ed131e4bb925cec465d91adfb0d9421 Mon Sep 17 00:00:00 2001 From: bobzel Date: Mon, 30 Sep 2024 18:26:59 -0400 Subject: fixed so that you can't set the author field from the schema view. fixed schema view to not trigger cell updates after cells have been unmounted (e.g, so that dragging a tab over a schema vew doesn't crash) --- .../collectionSchema/SchemaCellField.tsx | 190 +- .../views/nodes/DataVizBox/DocCreatorMenu.tsx | 2115 +++++++++++--------- 2 files changed, 1226 insertions(+), 1079 deletions(-) (limited to 'src') diff --git a/src/client/views/collections/collectionSchema/SchemaCellField.tsx b/src/client/views/collections/collectionSchema/SchemaCellField.tsx index 065544ac9..e26dd9646 100644 --- a/src/client/views/collections/collectionSchema/SchemaCellField.tsx +++ b/src/client/views/collections/collectionSchema/SchemaCellField.tsx @@ -1,22 +1,22 @@ -import { IReactionDisposer, action, computed, makeObservable, observable, reaction, runInAction } from "mobx"; -import { ObservableReactComponent } from "../../ObservableReactComponent"; -import { observer } from "mobx-react"; -import { OverlayView } from "../../OverlayView"; -import { DocumentIconContainer } from "../../nodes/DocumentIcon"; -import React, { FormEvent } from "react"; -import { FieldView, FieldViewProps } from "../../nodes/FieldView"; -import { ObjectField } from "../../../../fields/ObjectField"; -import { Doc } from "../../../../fields/Doc"; -import { DocumentView } from "../../nodes/DocumentView"; +import { IReactionDisposer, action, computed, makeObservable, observable, reaction, runInAction } from 'mobx'; +import { ObservableReactComponent } from '../../ObservableReactComponent'; +import { observer } from 'mobx-react'; +import { OverlayView } from '../../OverlayView'; +import { DocumentIconContainer } from '../../nodes/DocumentIcon'; +import React, { FormEvent } from 'react'; +import { FieldView, FieldViewProps } from '../../nodes/FieldView'; +import { ObjectField } from '../../../../fields/ObjectField'; +import { Doc } from '../../../../fields/Doc'; +import { DocumentView } from '../../nodes/DocumentView'; /** * The SchemaCellField renders text in schema cells while the user is editing, and updates the * contents of the field based on user input. It handles some cell-side logic for equations, such * as how equations are broken up within the text. - * - * The current implementation parses innerHTML to create spans based on the text in the cell. + * + * The current implementation parses innerHTML to create spans based on the text in the cell. * A more robust/safer approach would directly add elements in the react structure, but this - * has been challenging to implement. + * has been challenging to implement. */ export interface SchemaCellFieldProps { @@ -26,7 +26,7 @@ export interface SchemaCellFieldProps { oneLine?: boolean; Document: Doc; fieldKey: string; - refSelectModeInfo: {enabled: boolean, currEditing: SchemaCellField | undefined}; + refSelectModeInfo: { enabled: boolean; currEditing: SchemaCellField | undefined }; highlightCells?: (text: string) => void; GetValue(): string | undefined; SetValue(value: string, shiftDown?: boolean, enterKey?: boolean): boolean; @@ -35,7 +35,6 @@ export interface SchemaCellFieldProps { @observer export class SchemaCellField extends ObservableReactComponent { - private _disposers: { [name: string]: IReactionDisposer } = {}; private _inputref: HTMLDivElement | null = null; private _unrenderedContent: string = ''; @@ -48,7 +47,7 @@ export class SchemaCellField extends ObservableReactComponent { + setTimeout(() => { this._unrenderedContent = this._props.GetValue() ?? ''; this.setContent(this._unrenderedContent); }); //must be moved to end of batch or else other docs aren't loaded, so render as d-1 in function @@ -56,9 +55,11 @@ export class SchemaCellField extends ObservableReactComponent this.setCursorPosition(this._unrenderedContent.length)); - } + } }); } else { this._overlayDisposer?.(); @@ -104,10 +105,11 @@ export class SchemaCellField extends ObservableReactComponent this._props.GetValue(), fieldVal => { + console.log('Update: ' + this._props.Document.title, this._props.fieldKey, fieldVal); this._unrenderedContent = fieldVal ?? ''; this.finalizeEdit(false, false, false); } - ) + ); } componentDidUpdate(prevProps: Readonly) { @@ -120,7 +122,10 @@ export class SchemaCellField extends ObservableReactComponent disposer?.()); this.finalizeEdit(false, true, false); @@ -129,7 +134,7 @@ export class SchemaCellField extends ObservableReactComponent { const selfRef = text === this.selfRefPattern; return `${text}`; - } + }; makeSpans = (content: string) => { let chunkedText = content; @@ -144,28 +149,28 @@ export class SchemaCellField extends ObservableReactComponent { chunkedText = chunkedText.replace(match, this.generateSpan(match, cells.get(match))); - }) + }); return chunkedText; - } + }; /** - * Sets the rendered content of the cell to save user inputs. + * Sets the rendered content of the cell to save user inputs. * @param content the content to set * @param restoreCursorPos whether the cursor should be set back to where it was rather than the 0th index; should usually be true */ - @action + @action setContent = (content: string, restoreCursorPos?: boolean) => { const pos = this.cursorPosition; this._displayedContent = this.makeSpans(content); restoreCursorPos && setTimeout(() => this.setCursorPosition(pos)); - } + }; //Called from schemaview when a cell is selected to add a reference to the equation /** @@ -181,7 +186,7 @@ export class SchemaCellField extends ObservableReactComponent this.setCursorPosition(robustPos + text.length)); - } + }; @action setIsFocused = (value: boolean) => { @@ -195,32 +200,31 @@ export class SchemaCellField extends ObservableReactComponent { const selection = window.getSelection(); - if (!selection || position === null || !this._inputref) return; - + if (!selection || position === null || !this._inputref) return; + const range = document.createRange(); range.setStart(this._inputref, 0); range.collapse(true); - + let currentPos = 0; const setRange = (nodes: NodeList) => { for (let i = 0; i < nodes.length; ++i) { const node = nodes[i]; - if (node.nodeType === Node.TEXT_NODE) { + if (node.nodeType === Node.TEXT_NODE) { if (!node.textContent) return; const nextPos = currentPos + node.textContent.length; if (position <= nextPos) { @@ -231,11 +235,10 @@ export class SchemaCellField extends ObservableReactComponent) => { @@ -289,9 +292,12 @@ export class SchemaCellField extends ObservableReactComponent this.setupRefSelect(this.refSelectConditionMet), 0) + setTimeout(() => this.setupRefSelect(this.refSelectConditionMet), 0); break; case ' ': e.stopPropagation(); @@ -300,13 +306,16 @@ export class SchemaCellField extends ObservableReactComponent { this.setContent(this._unrenderedContent); setTimeout(() => this.setCursorPosition(cursorPos)); - } - , 0); + }, 0); break; case 'u': // for some reason 'u' otherwise exits the editor e.stopPropagation(); break; - case 'Shift': case 'Alt': case 'Meta': case 'Control': case ':': // prettier-ignore + case 'Shift': + case 'Alt': + case 'Meta': + case 'Control': + case ':': // prettier-ignore break; // eslint-disable-next-line no-fallthrough default: @@ -323,66 +332,63 @@ export class SchemaCellField extends ObservableReactComponent { + if (this._unmounted) { + return; + } if (this._unrenderedContent.replace(this.selfRefPattern, '') !== this._unrenderedContent) { - this._dependencyMessageShown ? this._dependencyMessageShown = false : - alert(`Circular dependency detected. Please update the field at ${this.selfRefPattern}.`) + if (this._dependencyMessageShown) { + this._dependencyMessageShown = false; + } else alert(`Circular dependency detected. Please update the field at ${this.selfRefPattern}.`); this._dependencyMessageShown = true; return; } this.setContent(this._unrenderedContent); - - if (this._props.SetValue(this._unrenderedContent, shiftDown, enterKey)) { - this._editing = false; - } else { - this._editing = false; - !lostFocus && - setTimeout( - action(() => { - this._editing = true; - }), - 0 - ); + + if (!this._props.SetValue(this._unrenderedContent, shiftDown, enterKey) && !lostFocus) { + setTimeout(action(() => (this._editing = true))); } - } + this._editing = false; + }; staticDisplay = () => { - return - { - // eslint-disable-next-line react/jsx-props-no-spreading - this._props.fieldContents ? : '' - } - - } + return {this._props.fieldContents ? : ''}; + }; renderEditor = () => { return ( -
{ this._inputref = r; }} - style={{ cursor: 'text', outline: 'none', overflow: 'auto', minHeight: `min(100%, ${(this._props.GetValue()?.split('\n').length || 1) * 15})`, minWidth: 20, }} - onBlur={() => {this._props.refSelectModeInfo.enabled ? setTimeout(() => {this.setIsFocused(true)}, 1000) : this.finalizeEdit(false, true, false)}} - autoFocus - onInput={this.onChange} - onKeyDown={this.onKeyDown} - onPointerDown={e => {e.stopPropagation(); setTimeout(() => this.setupRefSelect(this.refSelectConditionMet), 0)}} //timeout callback ensures that refSelectMode is properly set - onClick={e => e.stopPropagation} - onPointerUp={e => e.stopPropagation} - onPointerMove={e => {e.stopPropagation(); e.preventDefault()}} - dangerouslySetInnerHTML={{ __html: this._displayedContent }} - > -
+
{ + this._inputref = r; + }} + style={{ cursor: 'text', outline: 'none', overflow: 'auto', minHeight: `min(100%, ${(this._props.GetValue()?.split('\n').length || 1) * 15})`, minWidth: 20 }} + onBlur={() => (this._props.refSelectModeInfo.enabled ? setTimeout(() => this.setIsFocused(true), 1000) : this.finalizeEdit(false, true, false))} + autoFocus + onInput={this.onChange} + onKeyDown={this.onKeyDown} + onPointerDown={e => { + e.stopPropagation(); + setTimeout(() => this.setupRefSelect(this.refSelectConditionMet), 0); + }} //timeout callback ensures that refSelectMode is properly set + onClick={e => e.stopPropagation} + onPointerUp={e => e.stopPropagation} + onPointerMove={e => { + e.stopPropagation(); + e.preventDefault(); + }} + dangerouslySetInnerHTML={{ __html: this._displayedContent }}>
); - } + }; render() { const gval = this._props.GetValue()?.replace(/\n/g, '\\r\\n'); - if ((this._editing && gval !== undefined)) { + if (this._editing && gval !== undefined) { return
{this.renderEditor()}
; - } else return ( - this._props.contents instanceof ObjectField ? null : ( + } else + return this._props.contents instanceof ObjectField ? null : (
{this.staticDisplay()}
- ) - ); + ); } - -} \ No newline at end of file +} diff --git a/src/client/views/nodes/DataVizBox/DocCreatorMenu.tsx b/src/client/views/nodes/DataVizBox/DocCreatorMenu.tsx index 5d5c35dce..32aded9de 100644 --- a/src/client/views/nodes/DataVizBox/DocCreatorMenu.tsx +++ b/src/client/views/nodes/DataVizBox/DocCreatorMenu.tsx @@ -1,50 +1,42 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; -import { IReactionDisposer, ObservableMap, action, computed, makeObservable, observable, reaction, runInAction } from 'mobx'; +import { Colors } from 'browndash-components'; +import { action, computed, makeObservable, observable, reaction, runInAction } from 'mobx'; import { observer } from 'mobx-react'; +import { IDisposer } from 'mobx-utils'; import * as React from 'react'; -import { ClientUtils, returnAll, returnFalse, returnNone, returnOne, returnZero, setupMoveUpEvents } from '../../../../ClientUtils'; +import ReactLoading from 'react-loading'; +import { ClientUtils, returnFalse, setupMoveUpEvents } from '../../../../ClientUtils'; +import { emptyFunction } from '../../../../Utils'; import { Doc, NumListCast, StrListCast } from '../../../../fields/Doc'; -import { DocCast, ImageCast, ScriptCast, StrCast } from '../../../../fields/Types'; +import { Id } from '../../../../fields/FieldSymbols'; +import { DocCast, ImageCast, StrCast } from '../../../../fields/Types'; import { ImageField } from '../../../../fields/URLField'; -import { emptyFunction } from '../../../../Utils'; +import { Networking } from '../../../Network'; +import { GPTCallType, gptAPICall, gptImageCall } from '../../../apis/gpt/GPT'; +import { Docs } from '../../../documents/Documents'; +import { DragManager } from '../../../util/DragManager'; +import { MakeTemplate } from '../../../util/DropConverter'; import { SnappingManager } from '../../../util/SnappingManager'; import { UndoManager, undoable } from '../../../util/UndoManager'; +import { LightboxView } from '../../LightboxView'; import { ObservableReactComponent } from '../../ObservableReactComponent'; +import { CollectionFreeFormView } from '../../collections/collectionFreeForm/CollectionFreeFormView'; import { DocumentView, DocumentViewInternal } from '../DocumentView'; +import { FieldViewProps } from '../FieldView'; +import { OpenWhere } from '../OpenWhere'; import { DataVizBox } from './DataVizBox'; import './DocCreatorMenu.scss'; -import { Id } from '../../../../fields/FieldSymbols'; -import { Colors, IconButton, Size } from 'browndash-components'; -import { MakeTemplate } from '../../../util/DropConverter'; -import { DragManager } from '../../../util/DragManager'; -import { GPTCallType, gptAPICall, gptImageCall } from '../../../apis/gpt/GPT'; -import { CollectionFreeFormView } from '../../collections/collectionFreeForm/CollectionFreeFormView'; -import { Docs } from '../../../documents/Documents'; -import { OpenWhere } from '../OpenWhere'; -import { IDisposer } from 'mobx-utils'; -import { LightboxView } from '../../LightboxView'; -import ReactLoading from 'react-loading'; -import { CollectionStackingView } from '../../collections/CollectionStackingView'; -import { FieldViewProps } from '../FieldView'; -import { CollectionViewType } from '../../../documents/DocumentTypes'; -import { dropActionType } from '../../../util/DropActionTypes'; -import { ImageBox } from '../ImageBox'; -import { a } from '@react-spring/web'; -import { RichTextMenu } from '../formattedText/RichTextMenu'; -import e from 'cors'; -import { Networking } from '../../../Network'; export enum LayoutType { - Stacked = 'stacked', - Grid = 'grid', - Row = 'row', - Column = 'column', - Custom = 'custom' + Stacked = 'stacked', + Grid = 'grid', + Row = 'row', + Column = 'column', + Custom = 'custom', } @observer export class DocCreatorMenu extends ObservableReactComponent { - static Instance: DocCreatorMenu; private _disposers: { [name: string]: IDisposer } = {}; @@ -54,13 +46,13 @@ export class DocCreatorMenu extends ObservableReactComponent { @observable _templateDocs: Doc[] = []; @observable _selectedTemplate: Doc | undefined = undefined; @observable _columns: Col[] = []; - @observable _selectedCols: {title: string, type: string, desc: string}[] | undefined = []; + @observable _selectedCols: { title: string; type: string; desc: string }[] | undefined = []; - @observable _layout: {type: LayoutType, yMargin: number, xMargin: number, columns?: number, repeat: number} = {type: LayoutType.Grid, yMargin: 0, xMargin: 0, repeat: 0}; + @observable _layout: { type: LayoutType; yMargin: number; xMargin: number; columns?: number; repeat: number } = { type: LayoutType.Grid, yMargin: 0, xMargin: 0, repeat: 0 }; @observable _layoutPreview: boolean = true; @observable _layoutPreviewScale: number = 1; @observable _savedLayouts: DataVizTemplateLayout[] = []; - @observable _expandedPreview: {icon: ImageField, doc: Doc} | undefined = undefined; + @observable _expandedPreview: { icon: ImageField; doc: Doc } | undefined = undefined; @observable _suggestedTemplates: Doc[] = []; @observable _GPTOpt: boolean = false; @@ -76,7 +68,7 @@ export class DocCreatorMenu extends ObservableReactComponent { @observable _hoveredLayoutPreview: number | undefined = undefined; @observable _mouseX: number = -1; @observable _mouseY: number = -1; - @observable _startPos?: {x: number, y: number}; + @observable _startPos?: { x: number; y: number }; @observable _shouldDisplay: boolean = false; @observable _menuContent: 'templates' | 'options' | 'saved' | 'dashboard' = 'templates'; @@ -87,10 +79,10 @@ export class DocCreatorMenu extends ObservableReactComponent { @observable _snapPt: any; @observable _resizeHdlId: string = ''; @observable _resizing: boolean = false; - @observable _offset: {x: number, y: number} = {x: 0, y: 0}; + @observable _offset: { x: number; y: number } = { x: 0, y: 0 }; @observable _resizeUndo: UndoManager.Batch | undefined = undefined; - @observable _initDimensions: {width: number, height: number, x?: number, y?: number} = {width: 300, height: 400, x: undefined, y: undefined}; - @observable _menuDimensions: {width: number, height: number} = {width: 400, height: 400}; + @observable _initDimensions: { width: number; height: number; x?: number; y?: number } = { width: 300, height: 400, x: undefined, y: undefined }; + @observable _menuDimensions: { width: number; height: number } = { width: 400, height: 400 }; @observable _editing: boolean = false; constructor(props: any) { @@ -100,32 +92,40 @@ export class DocCreatorMenu extends ObservableReactComponent { //setTimeout(() => this.generateTemplates('')); } - @action setDataViz = (dataViz: DataVizBox) => { this._dataViz = dataViz }; - @action setTemplateDocs = (docs: Doc[]) => {this._templateDocs = docs.map(doc => doc.annotationOn ? DocCast(doc.annotationOn):doc)}; - @action setGSuggestedTemplates = (docs: Doc[]) => {this._suggestedTemplates = docs}; + @action setDataViz = (dataViz: DataVizBox) => { + this._dataViz = dataViz; + }; + @action setTemplateDocs = (docs: Doc[]) => { + this._templateDocs = docs.map(doc => (doc.annotationOn ? DocCast(doc.annotationOn) : doc)); + }; + @action setGSuggestedTemplates = (docs: Doc[]) => { + this._suggestedTemplates = docs; + }; @computed get docsToRender() { return this._selectedTemplate ? NumListCast(this._dataViz?.layoutDoc.dataViz_selectedRows) : []; } - @computed get rowsCount(){ + @computed get rowsCount() { switch (this._layout.type) { - case LayoutType.Row: case LayoutType.Stacked: + case LayoutType.Row: + case LayoutType.Stacked: return 1; case LayoutType.Column: return this.docsToRender.length; case LayoutType.Grid: return Math.ceil(this.docsToRender.length / (this._layout.columns ?? 1)) ?? 0; - default: + default: return 0; } } - @computed get columnsCount(){ + @computed get columnsCount() { switch (this._layout.type) { case LayoutType.Row: return this.docsToRender.length; - case LayoutType.Column: case LayoutType.Stacked: + case LayoutType.Column: + case LayoutType.Stacked: return 1; case LayoutType.Grid: return this._layout.columns ?? 0; @@ -140,31 +140,33 @@ export class DocCreatorMenu extends ObservableReactComponent { @computed get fieldsInfos(): Col[] { const colInfo = this._dataViz?.colsInfo; - return this.selectedFields.map(field => { - const fieldInfo = colInfo?.get(field); - - const col: Col = { - title: field, - type: fieldInfo?.type ?? TemplateFieldType.UNSET, - desc: fieldInfo?.desc ?? '', - sizes: fieldInfo?.sizes ?? [TemplateFieldSize.MEDIUM] - }; - - if (fieldInfo?.defaultContent !== undefined) { - col.defaultContent = fieldInfo.defaultContent; - } + return this.selectedFields + .map(field => { + const fieldInfo = colInfo?.get(field); + + const col: Col = { + title: field, + type: fieldInfo?.type ?? TemplateFieldType.UNSET, + desc: fieldInfo?.desc ?? '', + sizes: fieldInfo?.sizes ?? [TemplateFieldSize.MEDIUM], + }; + + if (fieldInfo?.defaultContent !== undefined) { + col.defaultContent = fieldInfo.defaultContent; + } - return col; - }).concat(this._columns); + return col; + }) + .concat(this._columns); } - @computed get canMakeDocs(){ + @computed get canMakeDocs() { return this._selectedTemplate !== undefined && this._layout !== undefined; } - get bounds(): {t: number, b: number, l: number, r: number} { + get bounds(): { t: number; b: number; l: number; r: number } { const rect = this._ref?.getBoundingClientRect(); - const bounds = {t: rect?.top ?? 0, b: rect?.bottom ?? 0, l: rect?.left ?? 0, r: rect?.right ?? 0}; + const bounds = { t: rect?.top ?? 0, b: rect?.bottom ?? 0, l: rect?.left ?? 0, r: rect?.right ?? 0 }; return bounds; } @@ -179,8 +181,8 @@ export class DocCreatorMenu extends ObservableReactComponent { clickEv.preventDefault(); func(); }, 'create docs') - ) - } + ); + }; @action onPointerDown = (e: PointerEvent) => { @@ -214,10 +216,21 @@ export class DocCreatorMenu extends ObservableReactComponent { componentDidMount() { document.addEventListener('pointerdown', this.onPointerDown, true); document.addEventListener('pointerup', this.onPointerUp); - this._disposers.templates = reaction(() => this._templateDocs.slice(), (docs) => docs.map(this.getIcon)); - this._disposers.gpt = reaction(() => this._suggestedTemplates.slice(), (docs) => docs.map(this.getIcon)); + this._disposers.templates = reaction( + () => this._templateDocs.slice(), + docs => docs.map(this.getIcon) + ); + this._disposers.gpt = reaction( + () => this._suggestedTemplates.slice(), + docs => docs.map(this.getIcon) + ); //this._disposers.columns = reaction(() => this._dataViz?.layoutDoc._dataViz_axes, () => {this.generateTemplates('')}) - this._disposers.lightbox = reaction(() => LightboxView.LightboxDoc(), doc => { doc ? this._shouldDisplay && this.closeMenu() : !this._shouldDisplay && this.openMenu()}); + this._disposers.lightbox = reaction( + () => LightboxView.LightboxDoc(), + doc => { + doc ? this._shouldDisplay && this.closeMenu() : !this._shouldDisplay && this.openMenu(); + } + ); //this._disposers.fields = reaction(() => this._dataViz?.axes, cols => this._selectedCols = cols?.map(col => { return {title: col, type: '', desc: ''}})) } @@ -227,12 +240,14 @@ export class DocCreatorMenu extends ObservableReactComponent { document.removeEventListener('pointerup', this.onPointerUp); } - updateIcons = (docs: Doc[]) => { docs.map(this.getIcon) } + updateIcons = (docs: Doc[]) => { + docs.map(this.getIcon); + }; @action updateSelectedCols = (cols: string[]) => { - this._selectedCols - } + this._selectedCols; + }; @action toggleDisplay = (x: number, y: number) => { @@ -246,10 +261,12 @@ export class DocCreatorMenu extends ObservableReactComponent { }; @action - closeMenu = () => { this._shouldDisplay = false }; + closeMenu = () => { + this._shouldDisplay = false; + }; @action - openMenu = () => { + openMenu = () => { const allTemplates = this._templateDocs.concat(this._suggestedTemplates); this._shouldDisplay = true; this.updateIcons(allTemplates); @@ -285,7 +302,7 @@ export class DocCreatorMenu extends ObservableReactComponent { await new Promise(res => { setTimeout(() => { res(this._interactionLock = undefined)})}); }); // prettier-ignore return true; - } + }; @action onDrag = (e: any): boolean => { @@ -294,12 +311,12 @@ export class DocCreatorMenu extends ObservableReactComponent { this._initDimensions.x = this._pageX; this._initDimensions.y = this._pageY; return true; - } + }; getResizeVals = (thisPt: { x: number; y: number }, dragHdl: string) => { const [w, h] = [this._initDimensions.width, this._initDimensions.height]; const [moveX, moveY] = [thisPt.x - this._snapPt.x, thisPt.y - this._snapPt.y]; - let vals: {scale: {x: number, y: number}, refPt: [number, number], transl: {x: number, y: number}}; + let vals: { scale: { x: number; y: number }; refPt: [number, number]; transl: { x: number; y: number } }; switch (dragHdl) { case 'topLeft': vals = { scale: { x: 1 - moveX / w, y: 1 -moveY / h }, refPt: [this.bounds.r, this.bounds.b], transl: {x: moveX, y: moveY } }; break; case 'topRight': vals = { scale: { x: 1 + moveX / w, y: 1 -moveY / h }, refPt: [this.bounds.l, this.bounds.b], transl: {x: 0, y: moveY } }; break; @@ -314,11 +331,11 @@ export class DocCreatorMenu extends ObservableReactComponent { return vals; }; - resizeView = (refPt: number[], scale: { x: number; y: number }, translation: {x: number, y: number}) => { - const refCent = [refPt[0], refPt[1]] // fixed reference point for resize (ie, a point that doesn't move) + resizeView = (refPt: number[], scale: { x: number; y: number }, translation: { x: number; y: number }) => { + const refCent = [refPt[0], refPt[1]]; // fixed reference point for resize (ie, a point that doesn't move) if (this._initDimensions.x === undefined) this._initDimensions.x = this._pageX; if (this._initDimensions.y === undefined) this._initDimensions.y = this._pageY; - const {height, width, x, y} = this._initDimensions; + const { height, width, x, y } = this._initDimensions; this._menuDimensions.width = Math.max(300, scale.x * width); this._menuDimensions.height = Math.max(200, scale.y * height); @@ -330,10 +347,10 @@ export class DocCreatorMenu extends ObservableReactComponent { const docView = DocumentView.getDocumentView(doc); if (docView) { docView.ComponentView?.updateIcon?.(); - return new Promise(res => setTimeout(() => res(ImageCast(docView.Document.icon)), 500));; + return new Promise(res => setTimeout(() => res(ImageCast(docView.Document.icon)), 500)); } return undefined; - }; + } @action updateSelectedTemplate = (template: Doc) => { if (this._selectedTemplate === template) { @@ -353,10 +370,7 @@ export class DocCreatorMenu extends ObservableReactComponent { }; isSelectedLayout = (layout: DataVizTemplateLayout) => { - return this._layout.xMargin === layout.layout.xMargin - && this._layout.yMargin === layout.layout.yMargin - && this._layout.type === layout.layout.type - && this._layout.columns === layout.columns; + return this._layout.xMargin === layout.layout.xMargin && this._layout.yMargin === layout.layout.yMargin && this._layout.type === layout.layout.type && this._layout.columns === layout.columns; }; @action @@ -365,7 +379,7 @@ export class DocCreatorMenu extends ObservableReactComponent { const origCount = this._callCount; let prompt: string = `(#${origCount}) Please generate for the fields:`; - this.selectedFields?.forEach(field => prompt += ` ${field},`) + this.selectedFields?.forEach(field => (prompt += ` ${field},`)); prompt += ` (-----NOT A FIELD-----) Additional prompt: ${inputText}`; this._GPTLoading = true; @@ -375,37 +389,43 @@ export class DocCreatorMenu extends ObservableReactComponent { if (res && this._callCount === origCount) { this._suggestedTemplates = []; - const templates: {template_type: string, fieldVals: {title: string, tlx: string, tly: string, brx: string, bry: string}[]}[] = JSON.parse(res); + const templates: { template_type: string; fieldVals: { title: string; tlx: string; tly: string; brx: string; bry: string }[] }[] = JSON.parse(res); this.createGeneratedTemplates(templates, 500, 500); } } catch (err) { console.error(err); - } + } }; @action - createGeneratedTemplates = (layouts: {template_type: string, fieldVals: {title: string, tlx: string, tly: string, brx: string, bry: string}[]}[], tempWidth: number, tempHeight: number) => { + createGeneratedTemplates = (layouts: { template_type: string; fieldVals: { title: string; tlx: string; tly: string; brx: string; bry: string }[] }[], tempWidth: number, tempHeight: number) => { const mainCollection = this._dataViz?.DocumentView?.().containerViewPath?.().lastElement()?.ComponentView as CollectionFreeFormView; const GPTTemplates: Doc[] = []; layouts.forEach(layout => { const fields: Doc[] = layout.fieldVals.map(field => { - const left: number = Number(field.tlx) * tempWidth / 2; const top: number = Number(field.tly) * tempHeight / 2; //prettier-ignore - const right: number = Number(field.brx) * tempWidth / 2; const bottom: number = Number(field.bry) * tempHeight / 2; //prettier-ignore - const height = bottom - top; - const width = right - left; - const doc = !field.title.includes('$$') ? Docs.Create.TextDocument('', { _height: height, _width: width, title: field.title, x: left, y: top, _text_fontSize: `${height/2}` }) : Docs.Create.ImageDocument('', { _height: height, _width: width, title: field.title.replace(/\$\$/g, ''), x: left, y: top }); + const left: number = (Number(field.tlx) * tempWidth) / 2; + const top: number = Number(field.tly) * tempHeight / 2; //prettier-ignore + const right: number = (Number(field.brx) * tempWidth) / 2; + const bottom: number = Number(field.bry) * tempHeight / 2; //prettier-ignore + const height = bottom - top; + const width = right - left; + const doc = !field.title.includes('$$') + ? Docs.Create.TextDocument('', { _height: height, _width: width, title: field.title, x: left, y: top, _text_fontSize: `${height / 2}` }) + : Docs.Create.ImageDocument('', { _height: height, _width: width, title: field.title.replace(/\$\$/g, ''), x: left, y: top }); return doc; }); const template = Docs.Create.FreeformDocument(fields, { _height: tempHeight, _width: tempWidth, title: layout.template_type, x: 400000, y: 400000 }); mainCollection.addDocument(template); - + GPTTemplates.push(template); }); - setTimeout(() => {this.setGSuggestedTemplates(GPTTemplates); /*GPTTemplates.forEach(template => mainCollection.removeDocument(template))*/}, 100); + setTimeout(() => { + this.setGSuggestedTemplates(GPTTemplates); /*GPTTemplates.forEach(template => mainCollection.removeDocument(template))*/ + }, 100); this.forceUpdate(); }; @@ -421,9 +441,7 @@ export class DocCreatorMenu extends ObservableReactComponent { this._templateDocs.splice(this._templateDocs.indexOf(doc), 1); }; - - - testTemplate = async() => { + testTemplate = async () => { // const temp = TemplateLayouts.FourField001; // const title: Doc = FieldFuncs.TextField({tl: temp.fields[0].tl, br: temp.fields[0].br}, temp.height, temp.width, 'title', 'Title', {backgroundColor: 'transparent'}); // const image: Doc = FieldFuncs.ImageField({tl: temp.fields[1].tl, br: temp.fields[1].br}, temp.height, temp.width, 'title', '', {borderColor: '#159fe4', borderWidth: '10', cornerRounding: 10, rotation: 40}); @@ -453,7 +471,7 @@ export class DocCreatorMenu extends ObservableReactComponent { try { const res = await gptImageCall('Image of panda eating a cookie'); - if (res){ + if (res) { const result = await Networking.PostToServer('/uploadRemoteImage', { sources: res }); console.log(result); @@ -461,15 +479,14 @@ export class DocCreatorMenu extends ObservableReactComponent { } catch (e) { console.log(e); } - }; @action addField = () => { - const newFields: Col[] = this._columns.concat([{title: '', type: TemplateFieldType.UNSET, desc: '', sizes: []}]) + const newFields: Col[] = this._columns.concat([{ title: '', type: TemplateFieldType.UNSET, desc: '', sizes: [] }]); this._columns = newFields; }; - @action removeField = (field: {title: string, type: string, desc: string}) => { + @action removeField = (field: { title: string; type: string; desc: string }) => { if (this._dataViz?.axes.includes(field.title)) { this._dataViz.selectAxes(this._dataViz.axes.filter(col => col !== field.title)); } else { @@ -483,11 +500,11 @@ export class DocCreatorMenu extends ObservableReactComponent { } if (this._columns.length === 1) { - this._columns = [] + this._columns = []; } else { this._columns.splice(this._columns.indexOf(toRemove[0]), 1); } - } + } }; @action setColTitle = (column: Col, title: string) => { @@ -530,13 +547,13 @@ export class DocCreatorMenu extends ObservableReactComponent { this.forceUpdate(); }; - generateGPTImage = async(prompt: string): Promise => { - console.log(prompt) + generateGPTImage = async (prompt: string): Promise => { + console.log(prompt); try { const res = await gptImageCall(prompt); - if (res){ + if (res) { const result = await Networking.PostToServer('/uploadRemoteImage', { sources: res }); const source = ClientUtils.prepend(result[0].accessPaths.agnostic.client); return source; @@ -544,12 +561,16 @@ export class DocCreatorMenu extends ObservableReactComponent { } catch (e) { console.log(e); } - } + }; matchesForTemplate = (template: TemplateDocInfos, cols: Col[]): number[][] => { - const colMatchesField = (col: Col, field : Field) => { return field.sizes?.some(size => col.sizes?.includes(size)) && field.types?.includes(col.type) }; + const colMatchesField = (col: Col, field: Field) => { + return field.sizes?.some(size => col.sizes?.includes(size)) && field.types?.includes(col.type); + }; - const matches: number[][] = Array(template.fields.length).fill([]).map(() => []); + const matches: number[][] = Array(template.fields.length) + .fill([]) + .map(() => []); template.fields.forEach((field, i) => { cols.forEach((col, v) => { @@ -576,8 +597,8 @@ export class DocCreatorMenu extends ObservableReactComponent { } } return false; - } - + }; + for (let v = 0; v < fieldsCt; ++v) { used.fill(false); augmentingPath(v); @@ -592,7 +613,6 @@ export class DocCreatorMenu extends ObservableReactComponent { return count; }; - findValidTemplates = (cols: Col[], templates: TemplateDocInfos[]) => { let validTemplates: any[] = []; templates.forEach(template => { @@ -602,7 +622,7 @@ export class DocCreatorMenu extends ObservableReactComponent { if (this.maxMatches(numFields, matches) === numFields) { validTemplates.push(template.title); } - }) + }); validTemplates = validTemplates.map(title => TemplateLayouts.getTemplateByTitle(title)); @@ -613,12 +633,12 @@ export class DocCreatorMenu extends ObservableReactComponent { // if (field.subfields) { // const doc = FieldFuncs.FreeformField({ - // tl: field.tl, - // br: field.br }, - // template.height, - // template.width, - // column.title, - // '', + // tl: field.tl, + // br: field.br }, + // template.height, + // template.width, + // column.title, + // '', // field.opts // ); @@ -634,10 +654,10 @@ export class DocCreatorMenu extends ObservableReactComponent { /** * Populates a preset template framework with content from a datavizbox or any AI-generated content. * @param template the preloaded template framework being filled in - * @param assignments a list of template field numbers (from top to bottom) and their assigned columns from the linked dataviz + * @param assignments a list of template field numbers (from top to bottom) and their assigned columns from the linked dataviz * @returns a doc containing the fully rendered template */ - fillPresetTemplate = async(template: TemplateDocInfos, assignments: {[field: string]: Col}): Promise => { + fillPresetTemplate = async (template: TemplateDocInfos, assignments: { [field: string]: Col }): Promise => { const wordLimit = (size: TemplateFieldSize) => { switch (size) { case TemplateFieldSize.TINY: @@ -650,44 +670,43 @@ export class DocCreatorMenu extends ObservableReactComponent { return 50; case TemplateFieldSize.HUGE: return 100; - default: + default: return 10; } - } + }; - const renderTextCalls = async(): Promise => { + const renderTextCalls = async (): Promise => { const rendered: Doc[] = []; if (GPTTextCalls.length) { - try { const prompt = fieldContent + GPTTextAssignment; - + const res = await gptAPICall(prompt, GPTCallType.FILL); - - if (res){ - - const assignments: {[title: string]: {number: string, content: string}} = JSON.parse(res); + + if (res) { + const assignments: { [title: string]: { number: string; content: string } } = JSON.parse(res); //console.log('assignments', GPTAssignments, 'assignment string', GPTAssignmentString, 'field content', fieldContent, 'response', res, 'assignments', assignments); Object.entries(assignments).forEach(([title, info]) => { const field: Field = template.fields[Number(info.number)]; const col = this.getColByTitle(title); - - const doc = FieldUtils.TextField({ - tl: field.tl, - br: field.br }, - template.height, - template.width, - col.title, - info.content ?? '', + + const doc = FieldUtils.TextField( + { + tl: field.tl, + br: field.br, + }, + template.height, + template.width, + col.title, + info.content ?? '', field.opts ); - + rendered.push(doc); }); - } - } catch(err) { + } catch (err) { console.log(err); } } @@ -695,23 +714,25 @@ export class DocCreatorMenu extends ObservableReactComponent { return rendered; }; - const createGeneratedImage = async(fieldNum: string, col: Col, prompt: string) => { + const createGeneratedImage = async (fieldNum: string, col: Col, prompt: string) => { const url = await this.generateGPTImage(prompt); const field: Field = template.fields[Number(fieldNum)]; - const doc = FieldUtils.ImageField({ - tl: field.tl, - br: field.br }, - template.height, - template.width, - col.title, - url ?? '', + const doc = FieldUtils.ImageField( + { + tl: field.tl, + br: field.br, + }, + template.height, + template.width, + col.title, + url ?? '', field.opts ); return doc; - } + }; - const renderImageCalls = async(): Promise => { + const renderImageCalls = async (): Promise => { const rendered: Doc[] = []; const calls = GPTIMGCalls; @@ -719,24 +740,28 @@ export class DocCreatorMenu extends ObservableReactComponent { try { const renderedImages: Doc[] = await Promise.all( calls.map(async ([fieldNum, col]) => { - const sysPrompt = 'Your job is to create a prompt for an AI image generator to help it generate an image based on existing content in a template and a user prompt. Your prompt should focus heavily on visual elements to help the image generator; avoid unecessary info that might distract it. ONLY INCLUDE THE PROMPT, NO OTHER TEXT OR EXPLANATION. The existing content is as follows: ' + fieldContent + ' **** The user prompt is: ' + col.desc; - + const sysPrompt = + 'Your job is to create a prompt for an AI image generator to help it generate an image based on existing content in a template and a user prompt. Your prompt should focus heavily on visual elements to help the image generator; avoid unecessary info that might distract it. ONLY INCLUDE THE PROMPT, NO OTHER TEXT OR EXPLANATION. The existing content is as follows: ' + + fieldContent + + ' **** The user prompt is: ' + + col.desc; + const prompt = await gptAPICall(sysPrompt, GPTCallType.COMPLETEPROMPT); console.log(sysPrompt, prompt); - + return createGeneratedImage(fieldNum, col, prompt); }) ); - + const renderedTemplates: Doc[] = await Promise.all(renderedImages); renderedTemplates.forEach(doc => rendered.push(doc)); - } catch (e){ + } catch (e) { console.log(e); } } return rendered; - } + }; const fields: Doc[] = []; @@ -748,9 +773,9 @@ export class DocCreatorMenu extends ObservableReactComponent { const stringifyGPTInfo = (calls: [string, Col][]): string => { let string: string = '*** COLUMN INFO:'; calls.forEach(([fieldNum, col]) => { - string += `--- title: ${col.title}, prompt: ${col.desc}, word limit: ${wordLimit(col.sizes[0])} words, assigned field: ${fieldNum} ---` + string += `--- title: ${col.title}, prompt: ${col.desc}, word limit: ${wordLimit(col.sizes[0])} words, assigned field: ${fieldNum} ---`; }); - return string += ' ***'; + return (string += ' ***'); }; const GPTTextAssignment = stringifyGPTInfo(GPTTextCalls); @@ -761,45 +786,49 @@ export class DocCreatorMenu extends ObservableReactComponent { const field: Field = template.fields[Number(f)]; const col = strCol[1]; - const doc = (col.type === TemplateFieldType.VISUAL ? FieldUtils.ImageField : FieldUtils.TextField)({ - tl: field.tl, - br: field.br }, - template.height, - template.width, - col.title, - col.defaultContent ?? '', + const doc = (col.type === TemplateFieldType.VISUAL ? FieldUtils.ImageField : FieldUtils.TextField)( + { + tl: field.tl, + br: field.br, + }, + template.height, + template.width, + col.title, + col.defaultContent ?? '', field.opts ); - fieldContent += `--- Field #${f} (title: ${col.title}): ${col.defaultContent ?? ''} ---` + fieldContent += `--- Field #${f} (title: ${col.title}): ${col.defaultContent ?? ''} ---`; fields.push(doc); }); template.decorations.forEach(dec => { - const doc = FieldUtils.FreeformField({ - tl: dec.tl, - br: dec.br }, - template.height, - template.width, - '', - '', - dec.opts, + const doc = FieldUtils.FreeformField( + { + tl: dec.tl, + br: dec.br, + }, + template.height, + template.width, + '', + '', + dec.opts ); fields.push(doc); }); const createMainDoc = (): Doc => { - const main = Docs.Create.FreeformDocument(fields, { - _height: template.height, - _width: template.width, - title: template.title, + const main = Docs.Create.FreeformDocument(fields, { + _height: template.height, + _width: template.width, + title: template.title, backgroundColor: template.opts.backgroundColor, - _layout_borderRounding: `${template.opts.cornerRounding}px` ?? '0px', + _layout_borderRounding: `${template.opts.cornerRounding}px` ?? '0px', borderWidth: template.opts.borderWidth, borderColor: template.opts.borderColor, - x: 40000, + x: 40000, y: 40000, }); @@ -807,40 +836,46 @@ export class DocCreatorMenu extends ObservableReactComponent { mainCollection.addDocument(main); return main; - } + }; const textCalls = await renderTextCalls(); const imageCalls = await renderImageCalls(); - textCalls.forEach(doc => {fields.push(doc)}); - imageCalls.forEach(doc => {fields.push(doc)}); + textCalls.forEach(doc => { + fields.push(doc); + }); + imageCalls.forEach(doc => { + fields.push(doc); + }); return createMainDoc(); - } + }; compileFieldDescriptions = (templates: TemplateDocInfos[]): string => { let descriptions: string = ''; templates.forEach(template => { - descriptions += `---------- NEW TEMPLATE TO INCLUDE: Description of template ${template.title}'s fields: ` + descriptions += `---------- NEW TEMPLATE TO INCLUDE: Description of template ${template.title}'s fields: `; template.fields.forEach((field, index) => { - descriptions += `{Field #${index}: ${field.description}} ` + descriptions += `{Field #${index}: ${field.description}} `; }); }); return descriptions; }; - compileColDescriptions = (cols: Col[]): string => { + compileColDescriptions = (cols: Col[]): string => { let descriptions: string = ' ------------- COL DESCRIPTIONS START HERE:'; - cols.forEach(col => descriptions += `{title: ${col.title}, sizes: ${String(col.sizes)}, type: ${col.type}, descreiption: ${col.desc}} `); + cols.forEach(col => (descriptions += `{title: ${col.title}, sizes: ${String(col.sizes)}, type: ${col.type}, descreiption: ${col.desc}} `)); return descriptions; }; - getColByTitle = (title: string) => { return this.fieldsInfos.filter(col => col.title === title)[0]; }; + getColByTitle = (title: string) => { + return this.fieldsInfos.filter(col => col.title === title)[0]; + }; @action - assignColsToFields = async(templates: TemplateDocInfos[], cols: Col[]): Promise<[TemplateDocInfos, {[field: number]: Col}][]> => { + assignColsToFields = async (templates: TemplateDocInfos[], cols: Col[]): Promise<[TemplateDocInfos, { [field: number]: Col }][]> => { const fieldDescriptions: string = this.compileFieldDescriptions(templates); const colDescriptions: string = this.compileColDescriptions(cols); @@ -857,24 +892,26 @@ export class DocCreatorMenu extends ObservableReactComponent { const res = await gptAPICall(prompt, GPTCallType.TEMPLATE); if (res && this._callCount === origCount) { - - const assignments: {[templateTitle: string]: {[field: string]: string}} = JSON.parse(res); - const brokenDownAssignments: [TemplateDocInfos, {[field: number]: Col}][] = []; + const assignments: { [templateTitle: string]: { [field: string]: string } } = JSON.parse(res); + const brokenDownAssignments: [TemplateDocInfos, { [field: number]: Col }][] = []; Object.entries(assignments).forEach(([tempTitle, assignment]) => { const template = TemplateLayouts.getTemplateByTitle(tempTitle); if (!template) return; - const toObj = Object.entries(assignment).reduce((a, [fieldNum, colTitle]) => { - a[Number(fieldNum)] = this.getColByTitle(colTitle); - return a; - }, {} as { [field: number]: Col }); - brokenDownAssignments.push([template, toObj]) - }) + const toObj = Object.entries(assignment).reduce( + (a, [fieldNum, colTitle]) => { + a[Number(fieldNum)] = this.getColByTitle(colTitle); + return a; + }, + {} as { [field: number]: Col } + ); + brokenDownAssignments.push([template, toObj]); + }); return brokenDownAssignments; } } catch (err) { console.error(err); - } + } return []; }; @@ -884,178 +921,200 @@ export class DocCreatorMenu extends ObservableReactComponent { const cols = this.fieldsInfos; const templates = this.findValidTemplates(cols, TemplateLayouts.allTemplates); - + const assignments: [TemplateDocInfos, { [field: number]: Col }][] = await this.assignColsToFields(templates, cols); - - const renderedTemplatePromises: Promise[] = assignments.map(([template, assignments]) => - this.fillPresetTemplate(template, assignments) - ); - + + const renderedTemplatePromises: Promise[] = assignments.map(([template, assignments]) => this.fillPresetTemplate(template, assignments)); + const renderedTemplates: Doc[] = await Promise.all(renderedTemplatePromises); - - setTimeout(() => { this.setGSuggestedTemplates(renderedTemplates); this._GPTLoading = false }); + + setTimeout(() => { + this.setGSuggestedTemplates(renderedTemplates); + this._GPTLoading = false; + }); }; - @action setExpandedView = (info: {icon: ImageField, doc: Doc} | undefined) => { + @action setExpandedView = (info: { icon: ImageField; doc: Doc } | undefined) => { this._expandedPreview = info; - } + }; - get templatesPreviewContents(){ + get templatesPreviewContents() { const renderedTemplates: Doc[] = []; - const GPTOptions = -
+ const GPTOptions =
; - // + // return (
- {this._expandedPreview ? -
- -
- - -
-
- : -
-
-
-
Suggested Templates
- +
-
400 ? 'center' : ''}}> - {this._GPTLoading ? ( -
- -
- ) : ( - this._suggestedTemplates?.map(doc => - ({icon: ImageCast(doc.icon), doc})).filter(info => info.icon && info.doc).map(info => -
this.setUpButtonClick(e, () => runInAction(() => this.updateSelectedTemplate(info.doc)))}> - - - -
- ))} -
-
-
-
+ ) : ( +
+
+
+
Suggested Templates
+
- {this._GPTOpt ? GPTOptions : null } -
-
-
-
-
-
Your Templates
- -
-
400 ? 'center' : ''}}> -
this.testTemplate()}> - +
400 ? 'center' : '' }}> + {this._GPTLoading ? ( +
+ +
+ ) : ( + this._suggestedTemplates + ?.map(doc => ({ icon: ImageCast(doc.icon), doc })) + .filter(info => info.icon && info.doc) + .map(info => ( +
this.setUpButtonClick(e, () => runInAction(() => this.updateSelectedTemplate(info.doc)))}> + + + +
+ )) + )} +
+
+
+ +
+ {this._GPTOpt ? GPTOptions : null} +
- {this._templateDocs.map(doc => ({icon: ImageCast(doc.icon), doc})).filter(info => info.icon && info.doc).map(info => { - if (renderedTemplates.includes(info.doc)) return undefined; - renderedTemplates.push(info.doc); - return (
this.setUpButtonClick(e, () => runInAction(() => this.updateSelectedTemplate(info.doc)))}> - - -
- )})} +
400 ? 'center' : '' }}> +
this.testTemplate()}> + +
+ {this._templateDocs + .map(doc => ({ icon: ImageCast(doc.icon), doc })) + .filter(info => info.icon && info.doc) + .map(info => { + if (renderedTemplates.includes(info.doc)) return undefined; + renderedTemplates.push(info.doc); + return ( +
this.setUpButtonClick(e, () => runInAction(() => this.updateSelectedTemplate(info.doc)))}> + + + +
+ ); + })} +
-
- } + )}
); } - get savedLayoutsPreviewContents(){ + get savedLayoutsPreviewContents() { return ( -
- {this._savedLayouts.map((layout, index) => -
this.setUpButtonClick(e, () => runInAction(() => this.updateSelectedSavedLayout(layout)))} - > - {this.layoutPreviewContents(87, layout, true, index)} -
- )} +
+ {this._savedLayouts.map((layout, index) => ( +
this.setUpButtonClick(e, () => runInAction(() => this.updateSelectedSavedLayout(layout)))}> + {this.layoutPreviewContents(87, layout, true, index)} +
+ ))}
); } - @action updateXMargin = (input: string) => { this._layout.xMargin = Number(input) }; - @action updateYMargin = (input: string) => { this._layout.yMargin = Number(input) }; - @action updateColumns = (input: string) => { this._layout.columns = Number(input) }; + @action updateXMargin = (input: string) => { + this._layout.xMargin = Number(input); + }; + @action updateYMargin = (input: string) => { + this._layout.yMargin = Number(input); + }; + @action updateColumns = (input: string) => { + this._layout.columns = Number(input); + }; get layoutConfigOptions() { const optionInput = (icon: string, func: Function, def?: number, key?: string, noMargin?: boolean) => { return ( -
-
- +
+
+
- func(e.currentTarget.value)} className='docCreatorMenu-input config layout-config'/> + func(e.currentTarget.value)} className="docCreatorMenu-input config layout-config" />
); - } + }; switch (this._layout.type) { case LayoutType.Row: - return ( -
- {optionInput('arrows-left-right', this.updateXMargin, this._layout.xMargin, '0')} -
- ); + return
{optionInput('arrows-left-right', this.updateXMargin, this._layout.xMargin, '0')}
; case LayoutType.Column: - return ( -
- {optionInput('arrows-up-down', this.updateYMargin, this._layout.yMargin, '1')} -
- ); + return
{optionInput('arrows-up-down', this.updateYMargin, this._layout.yMargin, '1')}
; case LayoutType.Grid: return ( -
+
{optionInput('arrows-up-down', this.updateYMargin, this._layout.xMargin, '2')} {optionInput('arrows-left-right', this.updateXMargin, this._layout.xMargin, '3')} {optionInput('table-columns', this.updateColumns, this._layout.columns, '4', true)} @@ -1072,9 +1131,7 @@ export class DocCreatorMenu extends ObservableReactComponent { // return Docs.Create.FreeformDocument([], { _height: 200, _width: 200, title: 'title'}); // } - screenToLocalTransform = () => - this._props - .ScreenToLocalTransform(); + screenToLocalTransform = () => this._props.ScreenToLocalTransform(); layoutPreviewContents = (outerSpan: number, altLayout?: DataVizTemplateLayout, small: boolean = false, id?: number) => { const doc: Doc | undefined = altLayout ? altLayout.template : this._selectedTemplate; @@ -1084,10 +1141,12 @@ export class DocCreatorMenu extends ObservableReactComponent { const docWidth: number = Number(doc._width); const docHeight: number = Number(doc._height); - const horizontalSpan: number = (docWidth + layout.xMargin) * (altLayout ? altLayout.columns : this.columnsCount) - layout.xMargin;; + const horizontalSpan: number = (docWidth + layout.xMargin) * (altLayout ? altLayout.columns : this.columnsCount) - layout.xMargin; const verticalSpan: number = (docHeight + layout.yMargin) * (altLayout ? altLayout.rows : this.rowsCount) - layout.yMargin; const largerSpan: number = horizontalSpan > verticalSpan ? horizontalSpan : verticalSpan; - const scaledDown = (input: number) => {return input / (largerSpan / outerSpan * this._layoutPreviewScale)} + const scaledDown = (input: number) => { + return input / ((largerSpan / outerSpan) * this._layoutPreviewScale); + }; const fontSize = Math.min(scaledDown(docWidth / 3), scaledDown(docHeight / 3)); return ( @@ -1114,220 +1173,275 @@ export class DocCreatorMenu extends ObservableReactComponent { // {null} // //
-
-
- - - {altLayout ? : null} + {altLayout ? ( + + ) : null}
- {
- {this._layout.type === LayoutType.Stacked ? -
- All -
: - this.docsToRender.map(num => -
this._dataViz?.setSpecialHighlightedRow(num)} - onMouseLeave={() => this._dataViz?.setSpecialHighlightedRow(undefined)} - className='docCreatorMenu-layout-preview-item' + { +
- {num} -
- )} - -
} + gridTemplateColumns: `repeat(${altLayout ? altLayout.columns : this.columnsCount}, ${scaledDown(docWidth)}px`, + gridTemplateRows: `${scaledDown(docHeight)}px`, + gridAutoRows: `${scaledDown(docHeight)}px`, + rowGap: `${scaledDown(layout.yMargin)}px`, + columnGap: `${scaledDown(layout.xMargin)}px`, + }}> + {this._layout.type === LayoutType.Stacked ? ( +
+ All +
+ ) : ( + this.docsToRender.map(num => ( +
this._dataViz?.setSpecialHighlightedRow(num)} + onMouseLeave={() => this._dataViz?.setSpecialHighlightedRow(undefined)} + className="docCreatorMenu-layout-preview-item" + style={{ + width: scaledDown(docWidth), + height: scaledDown(docHeight), + fontSize: fontSize, + }}> + {num} +
+ )) + )} +
+ }
); - } - - get optionsMenuContents(){ - const layoutEquals = (layout: DataVizTemplateLayout) => { + }; - } //TODO: ADD LATER + get optionsMenuContents() { + const layoutEquals = (layout: DataVizTemplateLayout) => {}; //TODO: ADD LATER const layoutOption = (option: LayoutType, optStyle?: {}, specialFunc?: Function) => { return ( -
this.setUpButtonClick(e, () => {specialFunc?.(); runInAction(() => this._layout.type = option)})}> + onPointerDown={e => + this.setUpButtonClick(e, () => { + specialFunc?.(); + runInAction(() => (this._layout.type = option)); + }) + }> {option}
); - } + }; const selectionBox = (width: number, height: number, icon: string, specClass?: string, options?: JSX.Element[], manual?: boolean): JSX.Element => { - return (
-
- + return ( +
+
+ +
+ {manual ? ( + + ) : ( + + )}
- {manual ? : - - } -
); - } + ); + }; const repeatOptions = [0, 1, 2, 3, 4, 5]; return ( -
-
-
+
+
+
{this._layout.type ? this._layout.type.toUpperCase() : 'Choose Layout'}
{layoutOption(LayoutType.Stacked)} - {layoutOption(LayoutType.Grid, undefined, () => {if (!this._layout.columns) this._layout.columns = Math.ceil(Math.sqrt(this.docsToRender.length))})} + {layoutOption(LayoutType.Grid, undefined, () => { + if (!this._layout.columns) this._layout.columns = Math.ceil(Math.sqrt(this.docsToRender.length)); + })} {layoutOption(LayoutType.Row)} {layoutOption(LayoutType.Column)} - {layoutOption(LayoutType.Custom, {borderBottom: `0px`})} + {layoutOption(LayoutType.Custom, { borderBottom: `0px` })}
-
- {this._layout.type ? this.layoutConfigOptions: null} - {this._layoutPreview ? this.layoutPreviewContents(this._menuDimensions.width * .75) : null} - {selectionBox(60, 20, 'repeat', undefined, repeatOptions.map(num => ))} -
-
+ {this._layout.type ? this.layoutConfigOptions : null} + {this._layoutPreview ? this.layoutPreviewContents(this._menuDimensions.width * 0.75) : null} + {selectionBox( + 60, + 20, + 'repeat', + undefined, + repeatOptions.map(num => ) + )} +
+
-
); } - get dashboardContents(){ + get dashboardContents() { const sizes: string[] = ['tiny', 'small', 'medium', 'large', 'huge']; const fieldPanel = (field: Col) => { return ( -
-
- {`${field.title} Field`} -
-
-
-
Title
-