aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/RichTextRules.ts
blob: f4c44e5cee5ea8786caa6e99c79a8471b331ef08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { textblockTypeInputRule, smartQuotes, emDash, ellipsis, InputRule } from "prosemirror-inputrules";
import { schema } from "./RichTextSchema";
import { wrappingInputRule } from "./prosemirrorPatches";
import { NodeSelection, TextSelection } from "prosemirror-state";
import { NumCast, Cast } from "../../new_fields/Types";
import { Doc } from "../../new_fields/Doc";
import { FormattedTextBox } from "../views/nodes/FormattedTextBox";
import { Docs } from "../documents/Documents";
import { Id } from "../../new_fields/FieldSymbols";

export const inpRules = {
    rules: [
        ...smartQuotes,
        ellipsis,
        emDash,

        // > blockquote
        wrappingInputRule(/^\s*>\s$/, schema.nodes.blockquote),

        // 1. ordered list
        wrappingInputRule(
            /^1\.\s$/,
            schema.nodes.ordered_list,
            () => {
                return ({ mapStyle: "decimal", bulletStyle: 1 });
            },
            (match: any, node: any) => {
                return node.childCount + node.attrs.order === +match[1];
            },
            (type: any) => ({ type: type, attrs: { mapStyle: "decimal", bulletStyle: 1 } })
        ),
        // a. alphabbetical list
        wrappingInputRule(
            /^a\.\s$/,
            schema.nodes.ordered_list,
            // match => {
            () => {
                return ({ mapStyle: "alpha", bulletStyle: 1 });
                // return ({ order: +match[1] })
            },
            (match: any, node: any) => {
                return node.childCount + node.attrs.order === +match[1];
            },
            (type: any) => ({ type: type, attrs: { mapStyle: "alpha", bulletStyle: 1 } })
        ),

        // * bullet list
        wrappingInputRule(/^\s*([-+*])\s$/, schema.nodes.bullet_list),

        // ``` code block
        textblockTypeInputRule(/^```$/, schema.nodes.code_block),

        // # heading
        textblockTypeInputRule(
            new RegExp(/^(#{1,6})\s$/),
            schema.nodes.heading,
            match => {
                return ({ level: match[1].length });
            }
        ),

        new InputRule(
            new RegExp(/^#([0-9]+)\s$/),
            (state, match, start, end) => {
                let size = Number(match[1]);
                let ruleProvider = FormattedTextBox.InputBoxOverlay!.props.ruleProvider;
                let heading = NumCast(FormattedTextBox.InputBoxOverlay!.props.Document.heading);
                if (ruleProvider && heading) {
                    (Cast(FormattedTextBox.InputBoxOverlay!.props.Document, Doc) as Doc).heading = Number(match[1]);
                    return state.tr.deleteRange(start, end);
                }
                return state.tr.deleteRange(start, end).addStoredMark(schema.marks.pFontSize.create({ fontSize: Number(match[1]) }));
            }),
        new InputRule(
            new RegExp(/t/),
            (state, match, start, end) => {
                if (state.selection.to === state.selection.from) return null;
                let node = (state.doc.resolve(start) as any).nodeAfter;
                return node ? state.tr.addMark(start, end, schema.marks.user_tag.create({ userid: Doc.CurrentUserEmail, tag: "todo", modified: Math.round(Date.now() / 1000 / 60) })) : state.tr;
            }),
        new InputRule(
            new RegExp(/i/),
            (state, match, start, end) => {
                if (state.selection.to === state.selection.from) return null;
                let node = (state.doc.resolve(start) as any).nodeAfter;
                return node ? state.tr.addMark(start, end, schema.marks.user_tag.create({ userid: Doc.CurrentUserEmail, tag: "ignore", modified: Math.round(Date.now() / 1000 / 60) })) : state.tr;
            }),
        new InputRule(
            new RegExp(/\!/),
            (state, match, start, end) => {
                if (state.selection.to === state.selection.from) return null;
                let node = (state.doc.resolve(start) as any).nodeAfter;
                return node ? state.tr.addMark(start, end, schema.marks.user_tag.create({ userid: Doc.CurrentUserEmail, tag: "important", modified: Math.round(Date.now() / 1000 / 60) })) : state.tr;
            }),
        new InputRule(
            new RegExp(/\x/),
            (state, match, start, end) => {
                if (state.selection.to === state.selection.from) return null;
                let node = (state.doc.resolve(start) as any).nodeAfter;
                return node ? state.tr.addMark(start, end, schema.marks.user_tag.create({ userid: Doc.CurrentUserEmail, tag: "disagree", modified: Math.round(Date.now() / 1000 / 60) })) : state.tr;
            }),
        new InputRule(
            new RegExp(/^\^\^\s$/),
            (state, match, start, end) => {
                let node = (state.doc.resolve(start) as any).nodeAfter;
                let sm = state.storedMarks || undefined;
                let ruleProvider = FormattedTextBox.InputBoxOverlay!.props.ruleProvider;
                let heading = NumCast(FormattedTextBox.InputBoxOverlay!.props.Document.heading);
                if (ruleProvider && heading) {
                    ruleProvider["ruleAlign_" + heading] = "center";
                    return node ? state.tr.deleteRange(start, end).setStoredMarks([...node.marks, ...(sm ? sm : [])]) : state.tr;
                }
                let replaced = node ? state.tr.replaceRangeWith(start, end, schema.nodes.paragraph.create({ align: "center" })).setStoredMarks([...node.marks, ...(sm ? sm : [])]) :
                    state.tr;
                return replaced.setSelection(new TextSelection(replaced.doc.resolve(end - 2)));
            }),
        new InputRule(
            new RegExp(/^\[\[\s$/),
            (state, match, start, end) => {
                let node = (state.doc.resolve(start) as any).nodeAfter;
                let sm = state.storedMarks || undefined;
                let ruleProvider = FormattedTextBox.InputBoxOverlay!.props.ruleProvider;
                let heading = NumCast(FormattedTextBox.InputBoxOverlay!.props.Document.heading);
                if (ruleProvider && heading) {
                    ruleProvider["ruleAlign_" + heading] = "left";
                    return node ? state.tr.deleteRange(start, end).setStoredMarks([...node.marks, ...(sm ? sm : [])]) : state.tr;
                }
                let replaced = node ? state.tr.replaceRangeWith(start, end, schema.nodes.paragraph.create({ align: "left" })).setStoredMarks([...node.marks, ...(sm ? sm : [])]) :
                    state.tr;
                return replaced.setSelection(new TextSelection(replaced.doc.resolve(end - 2)));
            }),
        new InputRule(
            new RegExp(/^\]\]\s$/),
            (state, match, start, end) => {
                let node = (state.doc.resolve(start) as any).nodeAfter;
                let sm = state.storedMarks || undefined;
                let ruleProvider = FormattedTextBox.InputBoxOverlay!.props.ruleProvider;
                let heading = NumCast(FormattedTextBox.InputBoxOverlay!.props.Document.heading);
                if (ruleProvider && heading) {
                    ruleProvider["ruleAlign_" + heading] = "right";
                    return node ? state.tr.deleteRange(start, end).setStoredMarks([...node.marks, ...(sm ? sm : [])]) : state.tr;
                }
                let replaced = node ? state.tr.replaceRangeWith(start, end, schema.nodes.paragraph.create({ align: "right" })).setStoredMarks([...node.marks, ...(sm ? sm : [])]) :
                    state.tr;
                return replaced.setSelection(new TextSelection(replaced.doc.resolve(end - 2)));
            }),
        new InputRule(
            new RegExp(/##\s$/),
            (state, match, start, end) => {
                let target = Docs.Create.TextDocument({ width: 75, height: 35, autoHeight: true, fontSize: 9, title: "inline comment" });
                let node = (state.doc.resolve(start) as any).nodeAfter;
                let newNode = schema.nodes.dashComment.create({ docid: target[Id] });
                let dashDoc = schema.nodes.dashDoc.create({ width: 75, height: 35, title: "dashDoc", docid: target[Id], float: "right" });
                let sm = state.storedMarks || undefined;
                let replaced = node ? state.tr.insert(start, newNode).replaceRangeWith(start + 1, end + 1, dashDoc).setStoredMarks([...node.marks, ...(sm ? sm : [])]) :
                    state.tr;
                return replaced;//.setSelection(new NodeSelection(replaced.doc.resolve(end)));
            }),
        new InputRule(
            new RegExp(/\(\(/),
            (state, match, start, end) => {
                let node = (state.doc.resolve(start) as any).nodeAfter;
                let sm = state.storedMarks || undefined;
                let mark = state.schema.marks.highlight.create();
                let selected = state.tr.setSelection(new TextSelection(state.doc.resolve(start), state.doc.resolve(end))).addMark(start, end, mark);
                let content = selected.selection.content();
                let replaced = node ? selected.replaceRangeWith(start, start,
                    schema.nodes.star.create({ visibility: true, text: content, textslice: content.toJSON() })).setStoredMarks([...node.marks, ...(sm ? sm : [])]) :
                    state.tr;
                return replaced.setSelection(new TextSelection(replaced.doc.resolve(end + 1)));
            }),
        new InputRule(
            new RegExp(/\)\)/),
            (state, match, start, end) => {
                let mark = state.schema.marks.highlight.create();
                return state.tr.removeStoredMark(mark);
            }),
        new InputRule(
            new RegExp(/\^f\s$/),
            (state, match, start, end) => {
                let newNode = schema.nodes.footnote.create({});
                let tr = state.tr;
                tr.deleteRange(start, end).replaceSelectionWith(newNode); // replace insertion with a footnote.
                return tr.setSelection(new NodeSelection( // select the footnote node to open its display
                    tr.doc.resolve(  // get the location of the footnote node by subtracting the nodesize of the footnote from the current insertion point anchor (which will be immediately after the footnote node)
                        tr.selection.anchor - tr.selection.$anchor.nodeBefore!.nodeSize)));
            }),
        // let newNode = schema.nodes.footnote.create({});
        // if (dispatch && state.selection.from === state.selection.to) {
        //     return true;
        // }
    ]
};