import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { Tooltip } from '@material-ui/core'; import { action, computed, observable } from 'mobx'; import { observer } from 'mobx-react'; import * as React from 'react'; import { ColorState, SketchPicker } from 'react-color'; import { Doc, StrListCast } from '../../../../fields/Doc'; import { createSchema, makeInterface } from '../../../../fields/Schema'; import { ScriptField } from '../../../../fields/ScriptField'; import { BoolCast, Cast, StrCast, ScriptCast } from '../../../../fields/Types'; import { SelectionManager } from '../../../util/SelectionManager'; import { ColorScheme } from '../../../util/SettingsManager'; import { undoBatch, UndoManager } from '../../../util/UndoManager'; import { ContextMenu } from '../../ContextMenu'; import { DocComponent } from '../../DocComponent'; import { StyleProp } from '../../StyleProvider'; import { FieldView, FieldViewProps } from '.././FieldView'; 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" } 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 dropdownDirection: string = StrCast(this.rootDoc.dropDownDirection); @observable private buttonList: string[] = StrListCast(this.rootDoc.btnList); @observable private activeFont: string = StrCast(Doc.UserDoc()._fontFamily); @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 **/ /** * 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}
); } @undoBatch setColor = action((color: ColorState, docColor?: string, userColor?: string) => { console.log(docColor, userColor); if (docColor) { const numSelected = SelectionManager.Views().length; const selectedDoc = numSelected > 0 ? SelectionManager.Views()[0].Document : undefined; eval(docColor); } else if (userColor) { const userDoc = Doc.UserDoc(); eval(userColor); } }); /** * 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}
} {/* */}
) } 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(); // Toggle and canClick properties as determined from the variable passed into the button doc const toggle = this.rootDoc.toggle ? ScriptCast(this.rootDoc.toggle) : undefined; const canClick: boolean = this.rootDoc.canClick ? eval(StrCast(this.rootDoc.canClick)) : false; // if (toggle) { // console.log(StrCast(this.rootDoc.title), toggle); // toggle.script.run(); // } 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}
; const dropdownCaret =
; const colorBox = (func: (color: ColorState) => void) => ; const items = this.buttonList.map((value) => { return
Doc.UserDoc()._fontFamily = value}> {value}
; }); /** * Menu Panel Button: menuBtn * Dropdown Button: dropDownBtn * doubleBtn **/ // CODEDUMP: glr // const presSize = type === ButtonType.MenuButton ? 30 : 25; // const presTrailsIcon = ; // TODO:glr Add label of button type let button = ( <> {this.defaultButton} ); switch (this.type) { case ButtonType.DropdownButton: button = ( <> {this.dropdownButton} ); break; case ButtonType.DropdownList: button = (
this.rootDoc.dropDownOpen = !this.rootDoc.dropDownOpen}> {toggle} {label} {dropdownCaret} {this.rootDoc.dropDownOpen ? <>
{items}
{ e.stopPropagation(); this.rootDoc.dropDownOpen = false; }} /> : null}
); break; case ButtonType.ColorButton: button = (
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((color) => this.setColor(color, StrCast(this.rootDoc.docColor), StrCast(this.rootDoc.userColor)))}
{ e.stopPropagation(); this.rootDoc.dropDownOpen = false; }} /> : null}
); break; case ButtonType.ToolButton: button = (
{label}
); break; case ButtonType.ToggleButton: button = (
{label}
); 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 ? button : {StrCast(this.layoutDoc.toolTip)}
}> {button} ; } }