diff options
author | yipstanley <stanley_yip@brown.edu> | 2019-01-20 19:48:08 -0500 |
---|---|---|
committer | yipstanley <stanley_yip@brown.edu> | 2019-01-20 19:48:08 -0500 |
commit | b0a8b59b967eae3d5812fd8e54e9f4f7a3fb80fc (patch) | |
tree | bc30e4bb0dc1cf5ec47c50e2e5317baf12294808 /src | |
parent | 481ca03c47962b301199285feebdb73a66818a9f (diff) | |
parent | 6cf622dda54e5b1793138c0492d71b574a6e8d75 (diff) |
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web into move_doc_get_out_the_way
Diffstat (limited to 'src')
-rw-r--r-- | src/Main.tsx | 1 | ||||
-rw-r--r-- | src/documents/Documents.ts | 2 | ||||
-rw-r--r-- | src/views/nodes/FieldTextBox.tsx | 82 |
3 files changed, 77 insertions, 8 deletions
diff --git a/src/Main.tsx b/src/Main.tsx index e417c0690..0a683c858 100644 --- a/src/Main.tsx +++ b/src/Main.tsx @@ -1,6 +1,7 @@ import * as React from 'react'; import * as ReactDOM from 'react-dom'; import "./Main.scss"; +import "normalize.css" import { NodeCollectionStore } from './stores/NodeCollectionStore'; import { StaticTextNodeStore } from './stores/StaticTextNodeStore'; import { VideoNodeStore } from './stores/VideoNodeStore'; diff --git a/src/documents/Documents.ts b/src/documents/Documents.ts index 442fb00c7..cc08f4123 100644 --- a/src/documents/Documents.ts +++ b/src/documents/Documents.ts @@ -44,7 +44,7 @@ export namespace Documents { export function TextDocument(text: string, options:DocumentOptions = {}): Document { let doc = GetTextPrototype().MakeDelegate(); setupOptions(doc, options); - doc.SetField(KeyStore.Data, new TextField(text)); + // doc.SetField(KeyStore.Data, new TextField(text)); return doc; } 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 |