import { IconProp } from '@fortawesome/fontawesome-svg-core'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { Tooltip } from '@material-ui/core'; import { action, computed, observable } from 'mobx'; import { observer } from 'mobx-react'; import { StringifyOptions } from 'querystring'; import * as React from 'react'; import { ColorState, SketchPicker } from 'react-color'; import { Doc, StrListCast } from '../../../../fields/Doc'; import { InkTool } from '../../../../fields/InkField'; import { createSchema, makeInterface } from '../../../../fields/Schema'; import { ScriptField } from '../../../../fields/ScriptField'; import { BoolCast, Cast, NumCast, StrCast } from '../../../../fields/Types'; import { DocumentType } from '../../../documents/DocumentTypes'; import { Scripting } from "../../../util/Scripting"; import { SelectionManager } from '../../../util/SelectionManager'; import { ColorScheme } from '../../../util/SettingsManager'; import { UndoManager } from '../../../util/UndoManager'; import { CollectionViewType } from '../../collections/CollectionView'; import { ContextMenu } from '../../ContextMenu'; import { DocComponent } from '../../DocComponent'; import { EditableView } from '../../EditableView'; import { Colors } from '../../global/globalEnums'; import { StyleProp } from '../../StyleProvider'; import { FieldView, FieldViewProps } from '.././FieldView'; import { RichTextMenu } from '../formattedText/RichTextMenu'; import './FontIconBox.scss'; const FontIconSchema = createSchema({ icon: "string", }); export enum ButtonType { MenuButton = "menuBtn", DropdownList = "drpdownList", DropdownButton = "drpdownBtn", ClickButton = "clickBtn", DoubleButton = "dblBtn", ToggleButton = "tglBtn", ColorButton = "colorBtn", ToolButton = "toolBtn", NumberButton = "numBtn", EditableText = "editableTxt" } export enum NumButtonType { Slider = "slider", DropdownOptions = "dropdown", Inline = "inline" } export interface ButtonProps extends FieldViewProps { type?: ButtonType; } type FontIconDocument = makeInterface<[typeof FontIconSchema]>; const FontIconDocument = makeInterface(FontIconSchema); @observer export class FontIconBox extends DocComponent(FontIconDocument) { public static LayoutString(fieldKey: string) { return FieldView.LayoutString(FontIconBox, fieldKey); } showTemplate = (): void => { const dragFactory = Cast(this.layoutDoc.dragFactory, Doc, null); dragFactory && this.props.addDocTab(dragFactory, "add:right"); } dragAsTemplate = (): void => { this.layoutDoc.onDragStart = ScriptField.MakeFunction('getCopy(this.dragFactory, true)'); }; useAsPrototype = (): void => { this.layoutDoc.onDragStart = ScriptField.MakeFunction('makeDelegate(this.dragFactory, true)'); }; specificContextMenu = (): void => { if (!Doc.UserDoc().noviceMode) { const cm = ContextMenu.Instance; cm.addItem({ description: "Show Template", event: this.showTemplate, icon: "tag" }); cm.addItem({ description: "Use as Render Template", event: this.dragAsTemplate, icon: "tag" }); cm.addItem({ description: "Use as Prototype", event: this.useAsPrototype, icon: "tag" }); } } // Determining UI Specs @observable private label = StrCast(this.rootDoc.label, StrCast(this.rootDoc.title)); @observable private icon = StrCast(this.dataDoc.icon, "user") as any; @observable private dropdown: boolean = BoolCast(this.rootDoc.dropDownOpen); @observable private buttonList: string[] = StrListCast(this.rootDoc.btnList); @observable private type = StrCast(this.rootDoc.btnType); /** * Types of buttons in dash: * - Main menu button (LHS) * - Tool button * - Expandable button (CollectionLinearView) * - Button inside of CollectionLinearView vs. outside of CollectionLinearView * - Action button * - Dropdown button * - Color button * - Dropdown list * - Number button **/ _batch: UndoManager.Batch | undefined = undefined; /** * Number button */ @computed get numberButton() { const numType: string = StrCast(this.rootDoc.numType); const setValue = (value: number) => { // Script for running the toggle const script: string = StrCast(this.rootDoc.script) + "(" + value + ")"; ScriptField.MakeScript(script)?.script.run(); }; // Script for checking the outcome of the toggle const checkScript: string = StrCast(this.rootDoc.script) + "(0, true)"; const checkResult: number = ScriptField.MakeScript(checkScript)?.script.run().result; if (numType === NumButtonType.Slider) { const dropdown =
{ this._batch = UndoManager.StartBatch("presDuration"); }} onPointerUp={() => { if (this._batch) this._batch.end(); }} onChange={(e: React.ChangeEvent) => { e.stopPropagation(); setValue(Number(e.target.value)); }} />
; return (
this.rootDoc.dropDownOpen = !this.rootDoc.dropDownOpen)} > {checkResult} {dropdown}
); } else if (numType === NumButtonType.DropdownOptions) { const items: number[] = []; for (let i = 0; i < 100; i++) { if (i % 2 === 0) { items.push(i); } } items.map((value) => { return
setValue(value)}> {value}
; }); return (
setValue(checkResult - 1))}>
e.stopPropagation()} onDoubleClick={action(() => this.rootDoc.dropDownOpen = !this.rootDoc.dropDownOpen)} > setValue(Number(e.target.value)))} />
setValue(checkResult + 1))}>
{this.rootDoc.dropDownOpen ?
{items}
{ e.stopPropagation(); this.rootDoc.dropDownOpen = false; }} />
: null}
); } else { return (
); } } /** * Dropdown button */ @computed get dropdownButton() { const active: string = StrCast(this.rootDoc.dropDownOpen); const color = this.props.styleProvider?.(this.rootDoc, this.props, StyleProp.Color); const backgroundColor = this.props.styleProvider?.(this.rootDoc, this.props, StyleProp.BackgroundColor); return (
this.rootDoc.dropDownOpen = !this.rootDoc.dropDownOpen)}> {!this.label || !Doc.UserDoc()._showLabel ? (null) :
{this.label}
}
{this.rootDoc.dropDownOpen ?
{/* DROPDOWN BOX CONTENTS */}
: null}
); } /** * Dropdown list */ @computed get dropdownListButton() { const active: string = StrCast(this.rootDoc.dropDownOpen); const color = this.props.styleProvider?.(this.rootDoc, this.props, StyleProp.Color); const backgroundColor = this.props.styleProvider?.(this.rootDoc, this.props, StyleProp.BackgroundColor); const script: string = StrCast(this.rootDoc.script); let noviceList: string[] = []; let text: string | undefined; let dropdown = true; let icon: IconProp = "caret-down"; if (script === 'changeView') { const selected = SelectionManager.Views().length ? SelectionManager.Views()[0] : undefined; if (selected && StrCast(selected.Document.type) === DocumentType.COL) { text = StrCast(selected.Document._viewType); } else if (selected) { dropdown = false; text = StrCast(selected.Document.type); icon = Doc.toIcon(selected.Document); } else { text = "None selected"; } noviceList = [CollectionViewType.Freeform, CollectionViewType.Schema, CollectionViewType.Stacking]; } else if (script === 'changeFont') { const selected = SelectionManager.Views().length ? SelectionManager.Views()[0] : undefined; if (selected && StrCast(selected.Document.type) === DocumentType.RTF) { text = StrCast(selected.Document._fontFamily); } else { const fontFamily = StrCast(Doc.UserDoc()._fontFamily); console.log(fontFamily); text = fontFamily; } noviceList = ["Roboto", "Times New Roman", "Arial", "Georgia", "Comic Sans MS", "Tahoma", "Impact", "Crimson Text"]; } const items = this.buttonList.map((value) => { if (Doc.UserDoc().noviceMode && !noviceList.includes(value)) { return; } const click = () => { const s = ScriptField.MakeScript(script + '("' + value + '")'); if (s) { s.script.run().result; } }; return
{value[0].toUpperCase() + value.slice(1)}
; }); const label = !this.label || !Doc.UserDoc()._showLabel ? (null) :
{this.label}
; return (
this.rootDoc.dropDownOpen = !this.rootDoc.dropDownOpen : undefined}>
{text && text[0].toUpperCase() + text.slice(1)}
{label}
{this.rootDoc.dropDownOpen ?
{items}
{ e.stopPropagation(); this.rootDoc.dropDownOpen = false; }} />
: null}
); } /** * Color button */ @computed get colorButton() { const active: string = StrCast(this.rootDoc.dropDownOpen); const color = this.props.styleProvider?.(this.rootDoc, this.props, StyleProp.Color); const backgroundColor = this.props.styleProvider?.(this.rootDoc, this.props, StyleProp.BackgroundColor); const numSelected = SelectionManager.Views().length; const selectedDoc = numSelected > 0 ? SelectionManager.Views()[0].Document : undefined; const script: string = StrCast(this.rootDoc.script); const scriptCheck: string = script + "(undefined, true)"; const boolResult = ScriptField.MakeScript(scriptCheck)?.script.run().result; console.log("[boolResul]: ", boolResult); const colorOptions: string[] = ['#D0021B', '#F5A623', '#F8E71C', '#8B572A', '#7ED321', '#417505', '#9013FE', '#4A90E2', '#50E3C2', '#B8E986', '#000000', '#4A4A4A', '#9B9B9B', '#FFFFFF', '#f1efeb', 'transparent']; const colorBox = (func: (color: ColorState) => void) => ; const label = !this.label || !Doc.UserDoc()._showLabel ? (null) :
{this.label}
; const dropdownCaret =
; const click = (value: ColorState) => { const hex: string = value.hex; const s = ScriptField.MakeScript(script + '("' + hex + '", false)'); if (s) { s.script.run().result; } }; return (
this.rootDoc.dropDownOpen = !this.rootDoc.dropDownOpen} onPointerDown={e => e.stopPropagation()}>
{label} {/* {dropdownCaret} */} {this.rootDoc.dropDownOpen ?
e.stopPropagation()} onClick={e => e.stopPropagation()} style={{ left: 0 }}> {colorBox(click)}
{ e.stopPropagation(); this.rootDoc.dropDownOpen = false; }} />
: null}
); } @computed get toggleButton() { // Determine the type of toggle button const switchToggle: boolean = BoolCast(this.rootDoc.switchToggle); // Script for running the toggle const script: string = StrCast(this.rootDoc.script) + "()"; // Script for checking the outcome of the toggle const checkScript: string = StrCast(this.rootDoc.script) + "(true)"; // Function to run the script const runScript = () => ScriptField.MakeScript(script)?.script.run(); const checkResult = ScriptField.MakeScript(checkScript)?.script.run().result; // Colors const color = this.props.styleProvider?.(this.rootDoc, this.props, StyleProp.Color); const backgroundColor = this.props.styleProvider?.(this.rootDoc, this.props, StyleProp.BackgroundColor); // Button label const label = !this.label || !Doc.UserDoc()._showLabel ? (null) :
{this.label}
; console.log("switchToggle"); return ( !switchToggle ?
{label}
:
); } /** * Default */ @computed get defaultButton() { const color = this.props.styleProvider?.(this.rootDoc, this.props, StyleProp.Color); const backgroundColor = this.props.styleProvider?.(this.rootDoc, this.props, StyleProp.BackgroundColor); const active: string = StrCast(this.rootDoc.dropDownOpen); return (
{!this.label || !Doc.UserDoc()._showLabel ? (null) :
{this.label}
}
); } @computed get editableText() { const setValue = (value: string, shiftDown?: boolean): boolean => { console.log("setValue"); return true; }; return
""} SetValue={setValue} contents="...">
; } render() { const color = this.props.styleProvider?.(this.rootDoc, this.props, StyleProp.Color); const backgroundColor = this.props.styleProvider?.(this.rootDoc, this.props, StyleProp.BackgroundColor); // Variables called through eval (from button) const canUndo: boolean = UndoManager.CanUndo(); const canRedo: boolean = UndoManager.CanRedo(); const numSelected = SelectionManager.Views().length; const selectedDoc = numSelected > 0 ? SelectionManager.Views()[0].Document : undefined; const userDoc = Doc.UserDoc(); const dark: boolean = Doc.UserDoc().colorScheme === ColorScheme.Dark; const active: string = StrCast(this.rootDoc.dropDownOpen); const label = !this.label || !Doc.UserDoc()._showLabel ? (null) :
{this.label}
; const menuLabel = !this.label || !Doc.UserDoc()._showMenuLabel ? (null) :
{this.label}
; // TODO:glr Add label of button type let button = this.defaultButton; switch (this.type) { case ButtonType.EditableText: button = this.editableText; case ButtonType.NumberButton: button = this.numberButton; break; case ButtonType.DropdownButton: button = this.dropdownButton; break; case ButtonType.DropdownList: button = this.dropdownListButton; break; case ButtonType.ColorButton: button = this.colorButton; break; case ButtonType.ToolButton: button = (
{label}
); break; case ButtonType.ToggleButton: button = this.toggleButton; break; case ButtonType.ClickButton: button = (
{label}
); break; case ButtonType.DoubleButton: button = (
{label}
); break; case ButtonType.MenuButton: button = (
{menuLabel}
); break; default: break; } return !this.layoutDoc.toolTip || this.type === ButtonType.DropdownList || this.type === ButtonType.ColorButton ? button : {StrCast(this.layoutDoc.toolTip)}
}> {button} ; } } // toggle: Set overlay status of selected document Scripting.addGlobal(function changeView(view: string) { const selected = SelectionManager.Views().length ? SelectionManager.Views()[0] : undefined; selected ? selected.Document._viewType = view : console.log("[FontIconBox.tsx] changeView failed"); }); // toggle: Set overlay status of selected document Scripting.addGlobal(function changeBackgroundColor(color?: string, checkResult?: boolean) { const selected = SelectionManager.Views().length ? SelectionManager.Views()[0] : undefined; if (checkResult) { return selected && selected.Document._backgroundColor; } selected ? selected.Document._backgroundColor = color : console.log("[FontIconBox.tsx] changeBackgroundColor failed"); }); // toggle: Set overlay status of selected document Scripting.addGlobal(function toggleOverlay(checkResult?: boolean) { const selected = SelectionManager.Views().length ? SelectionManager.Views()[0] : undefined; if (checkResult && selected) { return NumCast(selected.Document.z) === 1; } selected ? selected.props.CollectionFreeFormDocumentView?.().float() : console.log("[FontIconBox.tsx] toggleOverlay failed"); }); // toggle: Set overlay status of selected document Scripting.addGlobal(function changeFont(font: string) { SelectionManager.Views().map(dv => dv.props.Document._fontFamily = font); Doc.UserDoc()._fontFamily = font; console.log(Doc.UserDoc()._fontFamily); return Doc.UserDoc()._fontFamily; }); // toggle: Set overlay status of selected document Scripting.addGlobal(function changeFontColor(color?: string, checkResult?: boolean) { const selected = SelectionManager.Views().length ? SelectionManager.Views()[0] : undefined; if (checkResult) { if (selected) { console.log("[Font color] (selected): " + StrCast(selected.Document._fontColor)); return selected.Document._fontColor; } else { console.log("[Font color] (global): " + StrCast(Doc.UserDoc()._fontColor)); return Doc.UserDoc()._fontColor; } } Doc.UserDoc()._fontColor = color; }); // toggle: Set overlay status of selected document Scripting.addGlobal(function changeFontHighlight(color?: string, checkResult?: boolean) { const selected = SelectionManager.Views().length ? SelectionManager.Views()[0] : undefined; if (checkResult) { if (selected) { console.log("[Font color] (selected): " + StrCast(selected.Document._fontColor)); return selected.Document._fontColor; } else { console.log("[Font color] (global): " + StrCast(Doc.UserDoc()._fontColor)); return Doc.UserDoc()._fontColor; } } Doc.UserDoc()._fontColor = color; }); // toggle: Set overlay status of selected document Scripting.addGlobal(function changeFontSize(size: string, checkResult?: boolean) { if (checkResult) { const size: number = parseInt(StrCast(Doc.UserDoc()._fontSize), 10); return size; } console.log(size); Doc.UserDoc()._fontSize = size + "px"; }); Scripting.addGlobal(function toggleBold(checkResult?: boolean) { if (checkResult) { return Doc.UserDoc().bold; } SelectionManager.Views().map(dv => dv.props.Document.bold = !dv.props.Document.bold); Doc.UserDoc().bold = !Doc.UserDoc().bold; return Doc.UserDoc().bold; }); Scripting.addGlobal(function toggleUnderline(checkResult?: boolean) { if (checkResult) { return Doc.UserDoc().underline; } SelectionManager.Views().map(dv => dv.props.Document.underline = !dv.props.Document.underline); Doc.UserDoc().bold = !Doc.UserDoc().underline; return Doc.UserDoc().underline; }); Scripting.addGlobal(function toggleItalic(checkResult?: boolean) { if (checkResult) { return Doc.UserDoc().italic; } SelectionManager.Views().map(dv => dv.props.Document.italic = !dv.props.Document.italic); Doc.UserDoc().bold = !Doc.UserDoc().italic; return Doc.UserDoc().italic; }); Scripting.addGlobal(function setActiveInkTool(tool: InkTool, checkResult?: boolean) { if (checkResult) { return Doc.UserDoc().activeInkTool === tool; } if (tool) { Doc.UserDoc().activeInkTool = tool; } else { Doc.UserDoc().activeInkTool = InkTool.None; } });