diff options
author | bobzel <zzzman@gmail.com> | 2025-03-07 10:20:19 -0500 |
---|---|---|
committer | bobzel <zzzman@gmail.com> | 2025-03-07 10:20:19 -0500 |
commit | ccf4d9438012ed7d2e19e4a55166f1bc6ce3e952 (patch) | |
tree | 7abcd16999f6225bb5b677de89a6c94141803d3a /src/fields/RichTextField.ts | |
parent | 696ae072b1f00d616450d76bc016aebcc1c102fc (diff) |
cleaned up RTF creation w/ styles. fixed scripting compile bug when unused functions were considered not found.
Diffstat (limited to 'src/fields/RichTextField.ts')
-rw-r--r-- | src/fields/RichTextField.ts | 159 |
1 files changed, 41 insertions, 118 deletions
diff --git a/src/fields/RichTextField.ts b/src/fields/RichTextField.ts index 79ba34ada..63ae61c2f 100644 --- a/src/fields/RichTextField.ts +++ b/src/fields/RichTextField.ts @@ -42,128 +42,51 @@ export class RichTextField extends ObjectField { 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":[]}`, - '' - ); - } - static Initialize = (initial?: string) => { - const content: object[] = []; - const state = { - doc: { - type: 'doc', - content, - }, - selection: { - type: 'text', - anchor: 0, - head: 0, - }, - }; - if (initial && initial.length) { - content.push({ - type: 'paragraph', - content: { - type: 'text', - text: initial, - }, - }); - state.selection.anchor = state.selection.head = initial.length + 1; - } - return JSON.stringify(state); - }; - - private static ToProsemirrorState = (plainText: string, selectBack?: number, delimeter = '\n') => { - // Remap the text, creating blocks split on newlines - const elements = plainText.split(delimeter); - - // Google Docs adds in an extra carriage return automatically, so this counteracts it - !elements[elements.length - 1].length && elements.pop(); + // AARAV ADD= + static ToProsemirrorDoc = (content: Record<string, unknown>[], selection: Record<string, unknown>) => ({ + doc: { + type: 'doc', + content, + }, + selection, + }); - // Preserve the current state, but re-write the content to be the blocks - const parsed: Record<string, unknown> = JSON.parse(RichTextField.Initialize()); - (parsed.doc as Record<string, unknown>).content = elements.map(text => { - const paragraph: object = { - type: 'paragraph', - content: text.length ? [{ type: 'text', marks: [], text }] : undefined, // An empty paragraph gets treated as a line break - }; - return paragraph; - }); + private static ToProsemirrorTextContent = (text: string, styles?: { bold?: boolean; italic?: boolean; fontSize?: number; color?: string }) => [ + { + type: 'text', + marks: [ + ...(styles?.bold ? [{ type: 'strong' }] : []), + ...(styles?.italic ? [{ type: 'em' }] : []), + ...(styles?.fontSize ? [{ type: 'pFontSize', attrs: { fontSize: `${styles.fontSize}px` } }] : []), + ...(styles?.color ? [{ type: 'pFontColor', attrs: { fontColor: styles.color } }] : []), + ], + text, + }, + ]; - // 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: 2 + plainText.length - (selectBack ?? 0), head: 2 + plainText.length }; + private static ToProsemirrorDashDocContent = (docId: string) => [ + { + type: 'dashDoc', + attrs: { width: '200px', height: '200px', title: 'dashDoc', float: 'unset', hidden: false, docId }, + }, + ]; - // Export the ProseMirror-compatible state object we've just built - return JSON.stringify(parsed); - }; - - public static textToRtf(text: string, imgDocId?: string, selectBack?: number) { - return new RichTextField( - !imgDocId - ? this.ToProsemirrorState(text, selectBack) - : JSON.stringify({ - // this is a RichText json that has the question text placed above a related image - doc: { - type: 'doc', - content: [ - { - type: 'paragraph', - attrs: { align: 'center', color: null, id: null, indent: null, inset: null, lineSpacing: null, paddingBottom: null, paddingTop: null }, - content: [ - ...(text ? [{ type: 'text', text }] : []), - ...(imgDocId ? [{ type: 'dashDoc', attrs: { width: '200px', height: '200px', title: 'dashDoc', float: 'unset', hidden: false, docId: imgDocId } }] : []), - ], - }, - ], - }, - selection: { type: 'text', anchor: 2 + text.length, head: 2 + text.length }, - }), - text + private static ToProsemirror = (plaintext: string, imgDocId?: string, styles?: { bold?: boolean; italic?: boolean; fontSize?: number; color?: string }, selectBack?: number) => + RichTextField.ToProsemirrorDoc( + plaintext + .split('\n') + .filter(text => (imgDocId ? text : true)) // if there's an image doc, we don't want it repeat for each paragraph -- assume there's only one paragraph with text in it + .map(text => ({ + type: 'paragraph', + content: [ + ...(text.length ? RichTextField.ToProsemirrorTextContent(text, styles) : []), // An empty paragraph gets treated as a line break + ...(imgDocId ? RichTextField.ToProsemirrorDashDocContent(imgDocId) : []), + ], + })), + { type: 'text', anchor: 2 + plaintext.length - (selectBack ?? 0), head: 2 + plaintext.length } ); - } - // AARAV ADD - - public static textToRtfFormatting( - text: string, - imgDocId?: string, - selectBack?: number, - styles?: { bold?: boolean, italic?: boolean, fontSize?: number, color?: string } - ) { - return new RichTextField( - !imgDocId - ? this.ToProsemirrorState(text, selectBack) - : JSON.stringify({ - // This is the RichText JSON with the text and optional image - doc: { - type: 'doc', - content: [ - { - type: 'paragraph', - attrs: { align: 'center', color: null, id: null, indent: null, inset: null, lineSpacing: null, paddingBottom: null, paddingTop: null }, - content: [ - { - type: 'text', - text: text, - marks: [ - ...(styles?.bold ? [{ type: 'bold' }] : []), - ...(styles?.italic ? [{ type: 'italic' }] : []), - ...(styles?.fontSize ? [{ type: 'textStyle', style: `font-size:${styles.fontSize}px` }] : []), - ...(styles?.color ? [{ type: 'textStyle', style: `color:${styles.color}` }] : []), - ] - } - ] - } - ] - }, - selection: { type: 'text', anchor: 2 + text.length, head: 2 + text.length }, - }), - text - ); + public static textToRtf(text: string, imgDocId?: string, styles?: { bold?: boolean; italic?: boolean; fontSize?: number; color?: string }, selectBack?: number) { + return new RichTextField(JSON.stringify(RichTextField.ToProsemirror(text, imgDocId, styles, selectBack)), text); } - - - - } |