aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/documents/Documents.ts2
-rw-r--r--src/views/nodes/FieldTextBox.tsx82
2 files changed, 76 insertions, 8 deletions
diff --git a/src/documents/Documents.ts b/src/documents/Documents.ts
index bba678bbd..beee44bee 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