diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index d127bfdd8..daf8eba65 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -4,7 +4,8 @@ const React = require('react'); const createClass = require('create-react-class'); const _ = require('lodash'); const cx = require('classnames'); -const closeTag = require('./close-tag'); +const closeTag = require('./helpers/close-tag'); +const { WIDGET_TYPE, FIELD_TYPE } = require('./helpers/widget-elements/constants'); let CodeMirror; if(typeof navigator !== 'undefined'){ @@ -37,10 +38,36 @@ if(typeof navigator !== 'undefined'){ require('codemirror/addon/fold/xml-fold.js'); require('codemirror/addon/edit/closetag.js'); - const foldCode = require('./fold-code'); + const foldCode = require('./helpers/fold-code'); foldCode.registerHomebreweryHelper(CodeMirror); } +const themeWidgets = [{ + name : 'monster', + type : WIDGET_TYPE.SNIPPET, + classes : ['frame', 'wide'] +}, { + name : 'classTable', + type : WIDGET_TYPE.SNIPPET, + classes : ['frame', 'decoration', 'wide'] +}, { + name : 'image', + type : WIDGET_TYPE.IMAGE, + fields : [] +}, { + name : 'artist', + type : WIDGET_TYPE.SNIPPET, + fields : [{ + name : 'top', + type : FIELD_TYPE.STYLE, + increment : 5, + lineBreak : true + }] +}, { // catch all + name : '', + type : WIDGET_TYPE.SNIPPET +}]; + const CodeEditor = createClass({ displayName : 'CodeEditor', getDefaultProps : function() { @@ -49,13 +76,16 @@ const CodeEditor = createClass({ value : '', wrap : true, onChange : ()=>{}, - enableFolding : true + enableFolding : true, + theme : null }; }, getInitialState : function() { return { - docs : {} + docs : {}, + widgetUtils : {}, + focusedWidget : null }; }, @@ -91,6 +121,9 @@ const CodeEditor = createClass({ } else { this.codeMirror.setOption('foldOptions', false); } + + this.state.widgetUtils.updateWidgetGutter(); + this.state.widgetUtils.updateAllLineWidgets(); }, buildEditor : function() { @@ -155,7 +188,7 @@ const CodeEditor = createClass({ }, foldGutter : true, foldOptions : this.foldOptions(this.codeMirror), - gutters : ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'], + gutters : ['CodeMirror-linenumbers', 'CodeMirror-foldgutter', 'widget-gutter'], autoCloseTags : true, styleActiveLine : true, showTrailingSpace : false, @@ -169,14 +202,41 @@ const CodeEditor = createClass({ }); closeTag.autoCloseCurlyBraces(CodeMirror, this.codeMirror); + this.setState({ + widgetUtils : require('./helpers/widgets')(CodeMirror, themeWidgets, this.codeMirror) + }); + // Note: codeMirror passes a copy of itself in this callback. cm === this.codeMirror. Either one works. this.codeMirror.on('change', (cm)=>{this.props.onChange(cm.getValue());}); this.updateSize(); + + this.codeMirror.on('change', (cm)=>{ + this.props.onChange(cm.getValue()); + + this.state.widgetUtils.updateWidgetGutter(); + }); + + this.codeMirror.on('gutterClick', (cm, n)=>{ + const { gutterMarkers } = this.codeMirror.lineInfo(n); + + if(!!gutterMarkers && !!gutterMarkers['widget-gutter']) { + const { widgets } = this.codeMirror.lineInfo(n); + if(!widgets) { + this.state.widgetUtils.updateLineWidgets(n); + } else { + this.codeMirror.operation(()=>{ + for (const widget of widgets) { + this.state.widgetUtils.removeLineWidgets(widget); + } + }); + } + } + }); }, indent : function () { const cm = this.codeMirror; - if (cm.somethingSelected()) { + if(cm.somethingSelected()) { cm.execCommand('indentMore'); } else { cm.execCommand('insertSoftTab'); diff --git a/shared/naturalcrit/codeEditor/codeEditor.less b/shared/naturalcrit/codeEditor/codeEditor.less index 1334299e4..9fd9d7d73 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.less +++ b/shared/naturalcrit/codeEditor/codeEditor.less @@ -30,4 +30,21 @@ // background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAQAgMAAABW5NbuAAAACVBMVEVHcEwAAAAAAAAWawmTAAAAA3RSTlMAPBJ6PMxpAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAFUlEQVQI12NgwACcCQysASAEZGAAACMuAX06aCQUAAAAAElFTkSuQmCC) no-repeat right; // } //} + + .widget-gutter { + width: .7em; + } + + .snippet-options-widget { + background-color: lightblue; + padding: 2px 0 2px 0; + + * { + margin: 0 2px 0 2px; + } + + input { + max-width: 10vw; + } + } } diff --git a/shared/naturalcrit/codeEditor/close-tag.js b/shared/naturalcrit/codeEditor/helpers/close-tag.js similarity index 100% rename from shared/naturalcrit/codeEditor/close-tag.js rename to shared/naturalcrit/codeEditor/helpers/close-tag.js diff --git a/shared/naturalcrit/codeEditor/fold-code.js b/shared/naturalcrit/codeEditor/helpers/fold-code.js similarity index 100% rename from shared/naturalcrit/codeEditor/fold-code.js rename to shared/naturalcrit/codeEditor/helpers/fold-code.js diff --git a/shared/naturalcrit/codeEditor/helpers/widget-elements/constants.js b/shared/naturalcrit/codeEditor/helpers/widget-elements/constants.js new file mode 100644 index 000000000..dda0ab5ea --- /dev/null +++ b/shared/naturalcrit/codeEditor/helpers/widget-elements/constants.js @@ -0,0 +1,23 @@ +export const UNITS = ['cm', 'mm', 'in', 'px', 'pt', 'pc', 'em', 'ex', 'ch', 'rem', 'vw', 'vh', 'vmin', 'vmax', '%']; + +export const WIDGET_TYPE = { + SNIPPET : 0, + INLINE_SNIPPET : 1, + IMAGE : 2, +}; + +export const FIELD_TYPE = { + STYLE : 0 +}; + +export const PATTERNS = { + widget : { + [WIDGET_TYPE.SNIPPET] : (name)=>new RegExp(`^{{${name}(?:[^a-zA-Z].*)?`), + [WIDGET_TYPE.INLINE_SNIPPET] : (name)=>new RegExp(`{{${name}`), + [WIDGET_TYPE.IMAGE] : ()=>new RegExp(`^\\!\\[(?:[a-zA-Z -]+)?\\]\\(.*\\).*{[a-zA-Z0-9:, "'-]+}$`), + }, + field : { + [FIELD_TYPE.STYLE] : (name)=>new RegExp(`[{,;](${name}):((?:"[^},;"]*")|(?:[^},;]*))`), + }, + collectStyles : new RegExp(`(?:[{,;]([a-zA-Z-]+):)+`, 'g'), +}; \ No newline at end of file diff --git a/shared/naturalcrit/codeEditor/helpers/widget-elements/field/field.jsx b/shared/naturalcrit/codeEditor/helpers/widget-elements/field/field.jsx new file mode 100644 index 000000000..f945f8993 --- /dev/null +++ b/shared/naturalcrit/codeEditor/helpers/widget-elements/field/field.jsx @@ -0,0 +1,154 @@ +require('./field.less'); +const React = require('react'); +const createClass = require('create-react-class'); +const _ = require('lodash'); +const { UNITS } = require('../constants'); + +const Field = createClass({ + hintsRef : React.createRef(), + activeHintRef : React.createRef(), + + getDefaultProps : function() { + return { + field : {}, + n : 0, + value : '', + hints : [], + onChange : ()=>{} + }; + }, + + getInitialState : function() { + return { + value : '', + focused : false, + activeHint : null + }; + }, + + componentDidUpdate : function({ hints }) { + if(this.state.value !== this.props.value) { + this.setState({ + value : this.props.value + }); + } + + const hintsLength = this.props.hints.length; + if(hintsLength - 1 < this.state.activeHint && hintsLength !== hints.length) { + this.setState({ + activeHint : hintsLength === 0 ? 0 : hintsLength - 1 + }); + return; + } + + if(this.hintsRef.current && this.activeHintRef.current) { + const offset = this.activeHintRef.current.offsetTop; + const scrollTop = this.hintsRef.current.scrollTop; + if(scrollTop + 50 < offset || scrollTop + 50 > offset) { + this.hintsRef.current.scrollTo({ + top : offset - 50, + behavior : 'smooth' + }); + } + } + }, + + componentDidMount : function() { + this.setState({ + value : this.props.value + }); + }, + + change : function(e) { + this.props.onChange(e); + this.setState({ + value : e.target.value + }); + }, + + setFocus : function({ type }) { + if(type === 'focus') { + this.setState({ focused: true, activeHint: this.props.hints.length > 0 ? 0 : null }); + } else if(type === 'blur'){ + this.setState({ focused: false, activeHint: null }); + } + }, + + keyDown : function(e) { + const { code } = e; + const { value, activeHint } = this.state; + const { field, hints } = this.props; + const numberPattern = new RegExp(`([^-\\d]*)([-\\d]+)(${UNITS.join('|')})(.*)`); + const match = value.match(numberPattern); + if(code === 'ArrowDown') { + e.preventDefault(); + if(match) { + this.change({ + target : { + value : `${match.at(1) ?? ''}${Number(match[2]) - field.increment}${match[3]}${match.at(4) ?? ''}` + } + }); + } else { + this.setState({ + activeHint : activeHint === hints.length - 1 ? 0 : activeHint + 1 + }); + } + } else if(code === 'ArrowUp') { + e.preventDefault(); + if(match) { + this.change({ + target : { + value : `${match.at(1) ?? ''}${Number(match[2]) + field.increment}${match[3]}${match.at(4) ?? ''}` + } + }); + } else { + this.setState({ + activeHint : activeHint === 0 ? hints.length - 1 : activeHint - 1 + }); + } + } else if(code === 'Enter') { + e.preventDefault(); + if(!match) { + this.change({ + target : { + value : hints[activeHint] + } + }); + this.setState({ + activeHint : 0 + }); + } + } + }, + + render : function(){ + const { focused, value, activeHint } = this.state; + const { field, n } = this.props; + const hints = this.props.hints + .filter((h)=>h!==value) + .map((h, i)=>{ + if(activeHint === i) { + return