aboutsummaryrefslogtreecommitdiff
path: root/src/new_fields/RichTextField.ts
blob: 1b52e6f82905e531a720d3e290075d3813da58e4 (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
import { ObjectField } from "./ObjectField";
import { serializable } from "serializr";
import { Deserializable } from "../client/util/SerializationHelper";
import { Copy, ToScriptString } from "./FieldSymbols";
import { scriptingGlobal } from "../client/util/Scripting";

export const ToPlainText = Symbol("PlainText");
export const FromPlainText = Symbol("PlainText");
const delimiter = "\n";
const joiner = "";

@scriptingGlobal
@Deserializable("RichTextField")
export class RichTextField extends ObjectField {
    @serializable(true)
    readonly Data: string;

    constructor(data: string) {
        super();
        this.Data = data;
    }

    [Copy]() {
        return new RichTextField(this.Data);
    }

    [ToScriptString]() {
        return `new RichTextField("${this.Data}")`;
    }

    public static Initialize = (initial: string) => {
        !initial.length && (initial = " ");
        let pos = initial.length + 1;
        return `{"doc":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"${initial}"}]}]},"selection":{"type":"text","anchor":${pos},"head":${pos}}}`;
    }

    [ToPlainText]() {
        // Because we're working with plain text, just concatenate all paragraphs
        let content = JSON.parse(this.Data).doc.content;
        let paragraphs = content.filter((item: any) => item.type === "paragraph");

        // Functions to flatten ProseMirror paragraph objects (and their components) to plain text
        // While this function already exists in state.doc.textBeteen(), it doesn't account for newlines 
        let blockText = (block: any) => block.text;
        let concatenateParagraph = (p: any) => (p.content ? p.content.map(blockText).join(joiner) : "") + delimiter;

        // Concatentate paragraphs and string the result together
        let textParagraphs: string[] = paragraphs.map(concatenateParagraph);
        let plainText = textParagraphs.join(joiner);
        return plainText.substring(0, plainText.length - 1);
    }

    [FromPlainText](plainText: string) {
        // Remap the text, creating blocks split on newlines
        let elements = plainText.split(delimiter);

        // Google Docs adds in an extra carriage return automatically, so this counteracts it
        !elements[elements.length - 1].length && elements.pop();

        // Preserve the current state, but re-write the content to be the blocks
        let parsed = JSON.parse(this.Data);
        parsed.doc.content = elements.map(text => {
            let paragraph: any = { type: "paragraph" };
            text.length && (paragraph.content = [{ type: "text", marks: [], text }]); // An empty paragraph gets treated as a line break
            return paragraph;
        });

        // If the new content is shorter than the previous content and selection is unchanged, may throw an out of bounds exception, so we reset it
        parsed.selection = { type: "text", anchor: 1, head: 1 };

        // Export the ProseMirror-compatible state object we've jsut built
        return JSON.stringify(parsed);
    }

}