diff options
author | bobzel <zzzman@gmail.com> | 2025-07-29 16:31:09 -0400 |
---|---|---|
committer | bobzel <zzzman@gmail.com> | 2025-07-29 16:31:09 -0400 |
commit | 08d3b5c0208f8ec8e8c42a822c1793a30d107c3b (patch) | |
tree | 4a6cc9cdc7ecbf429ed4f0a99d13d9a2ee9e1165 | |
parent | de337e6bc0016bbdd9b1887d549d93fe1823bb9d (diff) |
tweaking scriptingBox to work somewhat again
-rw-r--r-- | src/client/views/ScriptBox.scss | 17 | ||||
-rw-r--r-- | src/client/views/ScriptBox.tsx | 152 | ||||
-rw-r--r-- | src/client/views/nodes/ScriptingBox.scss | 1 | ||||
-rw-r--r-- | src/client/views/nodes/ScriptingBox.tsx | 76 |
4 files changed, 38 insertions, 208 deletions
diff --git a/src/client/views/ScriptBox.scss b/src/client/views/ScriptBox.scss deleted file mode 100644 index 28326624a..000000000 --- a/src/client/views/ScriptBox.scss +++ /dev/null @@ -1,17 +0,0 @@ -.scriptBox-outerDiv { - width: 100%; - height: 100%; - display: flex; - flex-direction: column; -} - -.scriptBox-toolbar { - width: 100%; -} - -.scriptBox-textArea { - width: 100%; - height: 100%; - box-sizing: border-box; - resize: none; -}
\ No newline at end of file diff --git a/src/client/views/ScriptBox.tsx b/src/client/views/ScriptBox.tsx deleted file mode 100644 index c2fbef5a5..000000000 --- a/src/client/views/ScriptBox.tsx +++ /dev/null @@ -1,152 +0,0 @@ -import { action, makeObservable, observable } from 'mobx'; -import { observer } from 'mobx-react'; -import * as React from 'react'; -import { emptyFunction } from '../../Utils'; -import { Doc, Opt } from '../../fields/Doc'; -import { ScriptField } from '../../fields/ScriptField'; -import { ScriptCast } from '../../fields/Types'; -import { DragManager } from '../util/DragManager'; -import { CompileScript } from '../util/Scripting'; -import { EditableView } from './EditableView'; -import { OverlayView } from './OverlayView'; -import './ScriptBox.scss'; -import { DocumentIconContainer } from './nodes/DocumentIcon'; - -export interface ScriptBoxProps { - onSave: (text: string, onError: (error: string) => void) => void; - onCancel?: () => void; - initialText?: string; - showDocumentIcons?: boolean; - setParams?: (p: string[]) => void; -} - -@observer -export class ScriptBox extends React.Component<ScriptBoxProps> { - @observable - private _scriptText: string; - overlayDisposer?: () => void; - - constructor(props: ScriptBoxProps) { - super(props); - makeObservable(this); - this._scriptText = props.initialText || ''; - } - - @action - onChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => { - this._scriptText = e.target.value; - }; - - @action - onError = (error: string) => { - console.log('ScriptBox: ' + error); - }; - - onFocus = () => { - this.overlayDisposer?.(); - this.overlayDisposer = OverlayView.Instance.addElement(<DocumentIconContainer />, { x: 0, y: 0 }); - }; - - onBlur = () => { - this.overlayDisposer?.(); - }; - - render() { - let onFocus: Opt<() => void>; - let onBlur: Opt<() => void>; - if (this.props.showDocumentIcons) { - onFocus = this.onFocus; - onBlur = this.onBlur; - } - const params = ( - <EditableView - contents="" - display="block" - maxHeight={72} - height={35} - fontSize={28} - GetValue={() => ''} - SetValue={(value: string) => { - this.props.setParams?.(value.split(' ').filter(s => s !== ' ')); - return true; - }} - /> - ); - return ( - <div className="scriptBox-outerDiv"> - <div style={{ display: 'flex', flexDirection: 'column', height: '100%' }}> - <textarea className="scriptBox-textarea" onChange={this.onChange} value={this._scriptText} onFocus={onFocus} onBlur={onBlur} /> - <div style={{ background: 'beige' }}>{params}</div> - </div> - <div className="scriptBox-toolbar"> - <button - type="button" - onClick={e => { - this.props.onSave(this._scriptText, this.onError); - e.stopPropagation(); - }}> - Save - </button> - <button - type="button" - onClick={e => { - this.props.onCancel && this.props.onCancel(); - e.stopPropagation(); - }}> - Cancel - </button> - </div> - </div> - ); - } - // let l = docList(this.source[0].data).length; if (l) { let ind = this.target[0].index !== undefined ? (this.target[0].index+1) % l : 0; this.target[0].index = ind; this.target[0].proto = getProto(docList(this.source[0].data)[ind]);} - public static EditButtonScript(title: string, doc: Doc, fieldKey: string, clientX: number, clientY: number, contextParams?: { [name: string]: string }, defaultScript?: ScriptField) { - let overlayDisposer: () => void = emptyFunction; - const script = ScriptCast(doc[fieldKey]) || defaultScript; - let originalText: string | undefined; - if (script) { - originalText = script.script.originalScript; - } - // tslint:disable-next-line: no-unnecessary-callback-wrapper - const params: string[] = []; - const setParams = (p: string[]) => params.splice(0, params.length, ...p); - const scriptingBox = ( - <ScriptBox - initialText={originalText} - setParams={setParams} - onCancel={overlayDisposer} - onSave={(text, onError) => { - if (!text) { - doc['$' + fieldKey] = undefined; - } else { - const compScript = CompileScript(text, { - params: { this: Doc.name, ...contextParams }, - typecheck: false, - editable: true, - transformer: DocumentIconContainer.getTransformer(), - }); - if (!compScript.compiled) { - onError(compScript.errors.map(error => error.messageText).join('\n')); - return; - } - - const div = document.createElement('div'); - div.style.width = '90px'; - div.style.height = '20px'; - div.style.background = 'gray'; - div.style.position = 'absolute'; - div.style.display = 'inline-block'; - div.style.transform = `translate(${clientX}px, ${clientY}px)`; - div.innerHTML = 'button'; - params.length && DragManager.StartButtonDrag([div], text, doc.title + '-instance', {}, params, () => {}, clientX, clientY); - - doc['$' + fieldKey] = new ScriptField(compScript); - overlayDisposer(); - } - }} - showDocumentIcons - /> - ); - overlayDisposer = OverlayView.Instance.addWindow(scriptingBox, { x: 400, y: 200, width: 500, height: 400, title }); - } -} diff --git a/src/client/views/nodes/ScriptingBox.scss b/src/client/views/nodes/ScriptingBox.scss index de70dbe74..db45c501b 100644 --- a/src/client/views/nodes/ScriptingBox.scss +++ b/src/client/views/nodes/ScriptingBox.scss @@ -3,7 +3,6 @@ height: 100%; display: flex; flex-direction: column; - background-color: rgb(241, 239, 235); padding: 10px; .boxed { diff --git a/src/client/views/nodes/ScriptingBox.tsx b/src/client/views/nodes/ScriptingBox.tsx index 38a43e8d4..7887bab5d 100644 --- a/src/client/views/nodes/ScriptingBox.tsx +++ b/src/client/views/nodes/ScriptingBox.tsx @@ -126,7 +126,7 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() } @action - resetSuggestionPos(caret: { top: number; left: number; height: number }) { + resetSuggestionPos = (caret: { top: number; left: number; height: number }) => { if (!this._suggestionRef.current || !this._scriptTextRef.current) return; const suggestionWidth = this._suggestionRef.current.offsetWidth; const scriptWidth = this._scriptTextRef.current.offsetWidth; @@ -140,7 +140,7 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() this._suggestionBoxX = left; this._suggestionBoxY = top; - } + }; componentWillUnmount() { this._overlayDisposer?.(); @@ -305,7 +305,7 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() !existingOptions && ContextMenu.Instance.addItem({ description: 'Options...', subitems: options, icon: 'hand-point-right' }); }; - renderFunctionInputs() { + renderFunctionInputs = () => { const descriptionInput = ( <textarea className="scriptingBox-textarea-inputs" @@ -346,14 +346,14 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {this.renderErrorMessage()} </div> ); - } + }; - renderErrorMessage() { + renderErrorMessage = () => { return !this._errorMessage ? null : <div className="scriptingBox-errorMessage"> {this._errorMessage} </div>; - } + }; // rendering when a doc's value can be set in applied UI - renderDoc(parameter: string) { + renderDoc = (parameter: string) => { return ( <div className="scriptingBox-paramInputs" onFocus={this.onFocus} onBlur={() => this._overlayDisposer?.()} ref={ele => ele && this.createDashEventsTarget(ele, (e, de) => this.onDrop(e, de, parameter))}> <EditableView @@ -381,10 +381,10 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() /> </div> ); - } + }; // rendering when a string's value can be set in applied UI - renderBasicType(parameter: string, isNum: boolean) { + renderBasicType = (parameter: string, isNum: boolean) => { const strVal = isNum ? NumCast(this.dataDoc[parameter]).toString() : StrCast(this.dataDoc[parameter]); return ( <div className="scriptingBox-paramInputs" style={{ overflowY: 'hidden' }}> @@ -408,10 +408,10 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() /> </div> ); - } + }; // rendering when an enum's value can be set in applied UI (drop down box) - renderEnum(parameter: string, types: (string | boolean | number)[]) { + renderEnum = (parameter: string, types: (string | boolean | number)[]) => { return ( <div className="scriptingBox-paramInputs"> <div className="scriptingBox-viewBase"> @@ -432,10 +432,10 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() </div> </div> ); - } + }; // setting a parameter (checking type and name before it is added) - compileParam(value: string, whichParam?: number) { + compileParam = (value: string, whichParam?: number) => { if (value.includes(':')) { const ptype = value.split(':')[1].trim(); const pname = value.split(':')[0].trim(); @@ -457,10 +457,10 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() this._errorMessage = 'must set type of parameter'; } return false; - } + }; @action - handleToken(str: string) { + handleToken = (str: string) => { this._currWord = str; this._suggestions = []; this._scriptKeys.forEach((element: string) => { @@ -469,27 +469,27 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() } }); return this._suggestions; - } + }; @action - handleFunc(pos: number) { + handleFunc = (pos: number) => { const scriptString = this.rawText.slice(0, pos - 2); this._currWord = scriptString.split(' ')[scriptString.split(' ').length - 1]; this._suggestions = [StrCast(this._scriptingParams[this._currWord])]; return this._suggestions; - } + }; - getDescription(value: string) { + getDescription = (value: string) => { const descrip = this._scriptingDescriptions[value]; return descrip?.length > 0 ? descrip : ''; - } + }; - getParams(value: string) { + getParams = (value: string) => { const params = this._scriptingParams[value]; return params?.length > 0 ? params : ''; - } + }; - returnParam(item: string) { + returnParam = (item: string) => { const params = item.split(','); let value = ''; let first = true; @@ -502,15 +502,15 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() } }); return value; - } + }; - getSuggestedParams(pos: number) { + getSuggestedParams = (pos: number) => { const firstScript = this.rawText.slice(0, pos); const indexP = firstScript.lastIndexOf('.'); const indexS = firstScript.lastIndexOf(' '); const func = firstScript.slice((indexP > indexS ? indexP : indexS) + 1, firstScript.length + 1); return this._scriptingParams[func]; - } + }; @action suggestionPos = () => { @@ -524,7 +524,7 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() }; @action - keyHandler(e: React.KeyboardEvent, pos: number) { + keyHandler = (e: React.KeyboardEvent, pos: number) => { e.stopPropagation(); if (this._lastChar === 'Enter') { this.rawText += ' '; @@ -588,10 +588,10 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() </div> ); } - } + }; @action - handlePosChange(number: number) { + handlePosChange = (number: number) => { this._caretPos = number; if (this._caretPos === 0) { this.rawText = ' ' + this.rawText; @@ -601,7 +601,7 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() this.rawText = this.rawText.slice(0, this._caretPos - 1) + this.rawText.slice(this._caretPos, this.rawText.length); } } - } + }; @observable rawText: string = ''; @computed({ keepAlive: true }) get renderScriptingBox() { @@ -645,7 +645,7 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() ); } - renderFuncListElement(value: string | object) { + renderFuncListElement = (value: string | object) => { return typeof value !== 'string' ? null : ( <div> <div style={{ fontSize: '14px' }}>{value}</div> @@ -657,7 +657,7 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() </div> </div> ); - } + }; // inputs for scripting div (script box, params box, and params column) @computed get renderScriptingInputs() { @@ -704,7 +704,7 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() } // toolbar (with compile and apply buttons) for scripting UI - renderScriptingTools() { + renderScriptingTools = () => { const buttonStyle = 'scriptingBox-button' + (StrCast(this.Document.layout_fieldKey).startsWith('layout_on') ? '-finish' : ''); return ( <div className="scriptingBox-toolbar"> @@ -745,10 +745,10 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() )} </div> ); - } + }; // inputs UI for params which allows you to set values for each displayed in a list - renderParamsInputs() { + renderParamsInputs = () => { return ( <div className="scriptingBox-inputDiv" onPointerDown={e => this._props.isSelected() && e.stopPropagation()}> {!this.compileParams.length || !this.paramsNames ? null : ( @@ -775,10 +775,10 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {this.renderErrorMessage()} </div> ); - } + }; // toolbar (with edit and run buttons and error message) for params UI - renderTools(toolSet: string, func: () => void) { + renderTools = (toolSet: string, func: () => void) => { const buttonStyle = 'scriptingBox-button' + (StrCast(this.Document.layout_fieldKey).startsWith('layout_on') ? '-finish' : ''); return ( <div className="scriptingBox-toolbar"> @@ -810,7 +810,7 @@ export class ScriptingBox extends ViewBoxAnnotatableComponent<FieldViewProps>() )} </div> ); - } + }; // renders script UI if _applied = false and params UI if _applied = true render() { |