import { serializable } from 'serializr'; import { scriptingGlobal } from '../client/util/ScriptingGlobals'; import { Deserializable } from '../client/util/SerializationHelper'; import { Copy, ToJavascriptString, ToScriptString, ToString } from './FieldSymbols'; import { ObjectField } from './ObjectField'; @scriptingGlobal @Deserializable('RichTextField') export class RichTextField extends ObjectField { @serializable(true) readonly Data: string; @serializable(true) readonly Text: string; /** * NOTE: if 'text' doesn't match the plain text of 'data', this can cause infinite loop problems or other artifacts when rendered. * @param data this is the formatted text representation of the RTF * @param text this is the plain text of whatever text is in the 'data' */ constructor(data: string, text: string) { super(); this.Data = data; this.Text = text; // ideally, we'd compute 'text' from 'data' by doing what Prosemirror does at run-time ... just need to figure out how to write that function accurately } Empty() { return !(this.Text || this.Data.toString().includes('dashField') || this.Data.toString().includes('align')); } [Copy]() { return new RichTextField(this.Data, this.Text); } [ToJavascriptString]() { return '`' + this.Text + '`'; } [ToScriptString]() { return `new RichTextField(\`${this.Data?.replace(/"/g, '\\"')}\`, \`${this.Text}\`)`; } [ToString]() { return this.Text; } public static RTFfield() { return new RichTextField( `{"doc":{"type":"doc","content":[{"type":"paragraph","attrs":{"align":null,"color":null,"id":null,"indent":null,"inset":null,"lineSpacing":null,"paddingBottom":null,"paddingTop":null},"content":[]}]},"selection":{"type":"text","anchor":2,"head":2},"storedMarks":[]}`, '' ); } }