diff options
author | Tyler Schicke <tyler_schicke@brown.edu> | 2019-01-19 00:27:59 -0500 |
---|---|---|
committer | Tyler Schicke <tyler_schicke@brown.edu> | 2019-01-19 00:27:59 -0500 |
commit | 87495cce1e23b25312efc56a84b849babf367e82 (patch) | |
tree | b7d0805589d961701b2376415e7372fec0b383ed /src/views/nodes/FieldTextBox.tsx | |
parent | 657b07e5a0a4d73a0b9cd9b82b69178b1658ce8b (diff) |
Got basic version of ProseMirror working
Diffstat (limited to 'src/views/nodes/FieldTextBox.tsx')
-rw-r--r-- | src/views/nodes/FieldTextBox.tsx | 82 |
1 files changed, 75 insertions, 7 deletions
diff --git a/src/views/nodes/FieldTextBox.tsx b/src/views/nodes/FieldTextBox.tsx index 5df3e6012..dbac3906a 100644 --- a/src/views/nodes/FieldTextBox.tsx +++ b/src/views/nodes/FieldTextBox.tsx @@ -1,14 +1,21 @@ -import { Key } from "../../fields/Key"; +import { Key, KeyStore } from "../../fields/Key"; import { Document } from "../../fields/Document"; import { observer } from "mobx-react"; import { TextField } from "../../fields/TextField"; import React = require("react") -import { action, observable } from "mobx"; +import { action, observable, reaction, IReactionDisposer } from "mobx"; + +import {schema} from "prosemirror-schema-basic"; +import {EditorState, Transaction} from "prosemirror-state" +import {EditorView} from "prosemirror-view" +import {keymap} from "prosemirror-keymap" +import {baseKeymap} from "prosemirror-commands" +import {undo, redo, history} from "prosemirror-history" +import { Opt } from "../../fields/Field"; interface IProps { fieldKey:Key; doc:Document; - test:string; } // FieldTextBox: Displays an editable plain text node that maps to a specified Key of a Document @@ -28,13 +35,76 @@ interface IProps { // this will edit the document and assign the new value to that field. // @observer -export class FieldTextBox extends React.Component<IProps, IProps> { +export class FieldTextBox extends React.Component<IProps> { + private _ref: React.RefObject<HTMLDivElement>; + private _editorView: Opt<EditorView>; + private _reactionDisposer: Opt<IReactionDisposer>; constructor(props:IProps) { super(props); + + this._ref = React.createRef(); + this.onChange = this.onChange.bind(this); } + dispatchTransaction = (tx: Transaction) => { + if(this._editorView) { + const state = this._editorView.state.apply(tx); + this._editorView.updateState(state); + const {doc, fieldKey} = this.props; + doc.SetFieldValue(fieldKey, JSON.stringify(state.toJSON()), TextField); + } + } + + componentDidMount() { + let state:EditorState; + const {doc, fieldKey} = this.props; + const config = { + schema, + plugins: [ + history(), + keymap({"Mod-z": undo, "Mod-y": redo}), + keymap(baseKeymap) + ] + }; + + let field = doc.GetFieldT(fieldKey, TextField); + if(field) { + state = EditorState.fromJSON(config, JSON.parse(field.Data)); + } else { + state = EditorState.create(config); + } + if(this._ref.current) { + this._editorView = new EditorView(this._ref.current, { + state, + dispatchTransaction: this.dispatchTransaction + }); + } + + this._reactionDisposer = reaction(() => { + const field = this.props.doc.GetFieldT(this.props.fieldKey, TextField); + return field ? field.Data : undefined; + }, (field) => { + if(field && this._editorView) { + this._editorView.updateState(EditorState.fromJSON(config, JSON.parse(field))); + } + }) + } + + componentWillUnmount() { + if(this._editorView) { + this._editorView.destroy(); + } + if(this._reactionDisposer) { + this._reactionDisposer(); + } + } + + shouldComponentUpdate() { + return false; + } + @action onChange(e: React.ChangeEvent<HTMLInputElement>) { const {fieldKey, doc} = this.props; @@ -42,8 +112,6 @@ export class FieldTextBox extends React.Component<IProps, IProps> { } render() { - const {fieldKey, doc} = this.props; - const value = doc.GetFieldValue(fieldKey, TextField, String("")); - return (<input value={value} onChange={this.onChange} />) + return (<div ref={this._ref} />) } }
\ No newline at end of file |