diff options
Diffstat (limited to 'src/fields/RichTextField.ts')
-rw-r--r-- | src/fields/RichTextField.ts | 68 |
1 files changed, 43 insertions, 25 deletions
diff --git a/src/fields/RichTextField.ts b/src/fields/RichTextField.ts index dc636031a..bcef1fefc 100644 --- a/src/fields/RichTextField.ts +++ b/src/fields/RichTextField.ts @@ -25,7 +25,7 @@ export class RichTextField extends ObjectField { } Empty() { - return !(this.Text || this.Data.toString().includes('dashField') || this.Data.toString().includes('align')); + return !(this.Text || this.Data.toString().includes('dashField') || this.Data.toString().includes('dashDoc') || this.Data.toString().includes('align')); } [Copy]() { @@ -42,33 +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":[]}`, - '' - ); - } + // AARAV ADD= + static ToProsemirrorDoc = (content: Record<string, unknown>[], selection: Record<string, unknown>) => ({ + doc: { + type: 'doc', + content, + }, + selection, + }); + + 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, + }, + ]; - public static textToRtf(text: string, imgDocId?: string) { - return new RichTextField( - JSON.stringify({ - // this is a RichText json that has the question text placed above a related image - doc: { - type: 'doc', + private static ToProsemirrorDashDocContent = (docId: string) => [ + { + type: 'dashDoc', + attrs: { width: '200px', height: '200px', title: 'dashDoc', float: 'unset', hidden: false, docId }, + }, + ]; + + 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: [ - { - 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 } }] : []), - ], - }, + ...(text.length ? RichTextField.ToProsemirrorTextContent(text, styles) : []), // An empty paragraph gets treated as a line break + ...(imgDocId ? RichTextField.ToProsemirrorDashDocContent(imgDocId) : []), ], - }, - selection: { type: 'text', anchor: 2, head: 2 }, - }), - text + })), + { type: 'text', anchor: 2 + plaintext.length - (selectBack ?? 0), head: 2 + plaintext.length } ); + + 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); } } |