aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Zeleznik <zzzman@gmail.com>2020-01-28 23:43:53 -0500
committerBob Zeleznik <zzzman@gmail.com>2020-01-28 23:43:53 -0500
commit1801e65b9fb84070a65e74766782099e1fb6832b (patch)
tree182a168bc11abc01aad5f4d165b3bfede7b81a8e
parent2ec1a14b2ea19bd66d5f7525619bb9cdd38d39bd (diff)
added [[[doc:]?field]] to reference a doc field, and @@doc@@ for a hyperlink
-rw-r--r--src/client/util/RichTextRules.ts16
-rw-r--r--src/client/util/RichTextSchema.tsx20
2 files changed, 26 insertions, 10 deletions
diff --git a/src/client/util/RichTextRules.ts b/src/client/util/RichTextRules.ts
index d56a6a5fe..cdf7831a9 100644
--- a/src/client/util/RichTextRules.ts
+++ b/src/client/util/RichTextRules.ts
@@ -75,22 +75,28 @@ export const inpRules = {
new InputRule(
new RegExp(/\[\[([a-zA-Z_ \-0-9]+)\]\]$/),
(state, match, start, end) => {
- const fieldKey = match[1];
- const fieldView = state.schema.nodes.dashField.create({ fieldKey: StrCast(fieldKey) });
+ const fieldView = state.schema.nodes.dashField.create({ fieldKey: match[1] });
+ return state.tr.deleteRange(start, end).insert(start, fieldView);
+ }),
+ // create a text display of a metadata field
+ new InputRule(
+ new RegExp(/\[\[([a-zA-Z_ \-0-9]+):([a-zA-Z_ \-0-9]+)\]\]$/),
+ (state, match, start, end) => {
+ const fieldView = state.schema.nodes.dashField.create({ fieldKey: match[2], docid: match[1] });
return state.tr.deleteRange(start, end).insert(start, fieldView);
}),
// create a hyperlink portal
new InputRule(
- new RegExp(/\[\[:([a-zA-Z_ \-0-9]+)\]\]$/),
+ new RegExp(/@@([a-zA-Z_ \-0-9]+)@@$/),
(state, match, start, end) => {
- const docId = match[1].substring(1);
+ const docId = match[1];
DocServer.GetRefField(docId).then(docx => {
const target = ((docx instanceof Doc) && docx) || Docs.Create.FreeformDocument([], { title: docId, _width: 500, _height: 500, }, docId);
DocUtils.Publish(target, docId, returnFalse, returnFalse);
DocUtils.MakeLink({ doc: (schema as any).Document }, { doc: target }, "portal link", "");
});
const link = state.schema.marks.link.create({ href: Utils.prepend("/doc/" + docId), location: "onRight", title: docId, targetId: docId });
- return state.tr.addMark(start, end, link);
+ return state.tr.deleteRange(end - 1, end).deleteRange(start, start + 2).addMark(start, end - 3, link);
}),
// stop using active style
new InputRule(
diff --git a/src/client/util/RichTextSchema.tsx b/src/client/util/RichTextSchema.tsx
index c0400bd6c..8c9e5e263 100644
--- a/src/client/util/RichTextSchema.tsx
+++ b/src/client/util/RichTextSchema.tsx
@@ -1,4 +1,4 @@
-import { reaction, IReactionDisposer } from "mobx";
+import { reaction, IReactionDisposer, observable, runInAction } from "mobx";
import { baseKeymap, toggleMark } from "prosemirror-commands";
import { redo, undo } from "prosemirror-history";
import { keymap } from "prosemirror-keymap";
@@ -20,6 +20,7 @@ import { BoolCast, NumCast, StrCast } from "../../new_fields/Types";
import { FormattedTextBox } from "../views/nodes/FormattedTextBox";
import { ObjectField } from "../../new_fields/ObjectField";
import { ComputedField } from "../../new_fields/ScriptField";
+import { observer } from "mobx-react";
const blockquoteDOM: DOMOutputSpecArray = ["blockquote", 0], hrDOM: DOMOutputSpecArray = ["hr"],
preDOM: DOMOutputSpecArray = ["pre", ["code", 0]], brDOM: DOMOutputSpecArray = ["br"], ulDOM: DOMOutputSpecArray = ["ul", 0];
@@ -181,6 +182,7 @@ export const nodes: { [index: string]: NodeSpec } = {
inline: true,
attrs: {
fieldKey: { default: "" },
+ docid: { default: "" }
},
group: "inline",
draggable: false,
@@ -831,12 +833,14 @@ export class DashDocView {
}
}
+
export class DashFieldView {
_fieldWrapper: HTMLDivElement;
_labelSpan: HTMLSpanElement;
_fieldSpan: HTMLSpanElement;
_reactionDisposer: IReactionDisposer | undefined;
_textBoxDoc: Doc;
+ @observable _dashDoc: Doc | undefined;
constructor(node: any, view: any, getPos: any, tbox: FormattedTextBox) {
this._textBoxDoc = tbox.props.Document;
@@ -856,15 +860,21 @@ export class DashFieldView {
this._labelSpan.style.fontWeight = "bold";
this._labelSpan.style.fontSize = "larger";
this._labelSpan.innerHTML = `${node.attrs.fieldKey}: `;
- const ddoc = tbox.props.DataDoc || tbox.dataDoc;
- this._reactionDisposer && this._reactionDisposer();
- this._reactionDisposer = reaction(() => ddoc[node.attrs.fieldKey], fval => this._fieldSpan.innerHTML = Field.toString(fval as Field), { fireImmediately: true });
+ if (node.attrs.docid) {
+ const self = this;
+ DocServer.GetRefField(node.attrs.docid).then(async dashDoc => dashDoc instanceof Doc && runInAction(() => self._dashDoc = dashDoc));
+ } else {
+ this._dashDoc = tbox.props.DataDoc || tbox.dataDoc;
+ }
+ this._reactionDisposer?.();
+ this._reactionDisposer = reaction(() => this._dashDoc?.[node.attrs.fieldKey], fval => this._fieldSpan.innerHTML = Field.toString(fval as Field), { fireImmediately: true });
+
this._fieldWrapper.appendChild(this._labelSpan);
this._fieldWrapper.appendChild(this._fieldSpan);
(this as any).dom = this._fieldWrapper;
}
destroy() {
- this._reactionDisposer && this._reactionDisposer();
+ this._reactionDisposer?.();
}
}