diff options
-rw-r--r-- | src/client/documents/Documents.ts | 2 | ||||
-rw-r--r-- | src/client/util/Scripting.ts | 10 | ||||
-rw-r--r-- | src/client/views/nodes/formattedText/DailyJournal.tsx | 12 | ||||
-rw-r--r-- | src/fields/Doc.ts | 4 | ||||
-rw-r--r-- | src/fields/RichTextField.ts | 159 |
5 files changed, 55 insertions, 132 deletions
diff --git a/src/client/documents/Documents.ts b/src/client/documents/Documents.ts index d21fa04d4..e1aa9aca2 100644 --- a/src/client/documents/Documents.ts +++ b/src/client/documents/Documents.ts @@ -927,7 +927,7 @@ export namespace Docs { return InstanceFromProto( Prototypes.get(DocumentType.JOURNAL), - typeof text === 'string' ? RichTextField.textToRtfFormatting(text, undefined, undefined, styles) : text, + typeof text === 'string' ? RichTextField.textToRtf(text, undefined, styles, undefined) : text, { title: new Date().toLocaleDateString(undefined, { weekday: 'long', diff --git a/src/client/util/Scripting.ts b/src/client/util/Scripting.ts index 5d78c2fab..c6d98496a 100644 --- a/src/client/util/Scripting.ts +++ b/src/client/util/Scripting.ts @@ -48,7 +48,11 @@ export function isCompileError(toBeDetermined: CompileResult): toBeDetermined is // eslint-disable-next-line no-use-before-define function Run(script: string | undefined, customParams: string[], diagnostics: ts.Diagnostic[], originalScript: string, options: ScriptOptions): CompileResult { - const errors = diagnostics.filter(diag => diag.category === ts.DiagnosticCategory.Error).filter(diag => diag.code !== 2304 && diag.code !== 2339); + const errors = diagnostics.filter(diag => diag.category === ts.DiagnosticCategory.Error).filter(diag => // + diag.code !== 2304 && + diag.code !== 2339 && + (diag.code !== 2552 ||!Object.keys(scriptingGlobals).includes(diagnostics[0].messageText.toString().match(/Cannot find name '([A-Za-z0-9$-_]+)'/)?.[1]??"-------")) + ); // prettier-ignore if ((options.typecheck !== false && errors.length) || !script) { return { compiled: false, errors }; } @@ -222,10 +226,10 @@ export function CompileScript(script: string, options: ScriptOptions = {}): Comp if ('this' in params || 'this' in capturedVariables) { paramNames.push('this'); } - paramNames.push(...Object.keys(params).filter(p => p!== 'this' && !Object.keys(capturedVariables).includes(p))); + paramNames.push(...Object.keys(params).filter(p => p !== 'this' && !Object.keys(capturedVariables).includes(p))); const paramList = paramNames.map(key => { - const val = typeof params[key] === "string" && params[key].length && !"\"'`".includes(params[key][0]) ? `"${params[key]}"` : params[key]; + const val = typeof params[key] === 'string' && params[key].length && !'"\'`'.includes(params[key][0]) ? `"${params[key]}"` : params[key]; return `${key}: ${val}`; }); for (const key in capturedVariables) { diff --git a/src/client/views/nodes/formattedText/DailyJournal.tsx b/src/client/views/nodes/formattedText/DailyJournal.tsx index 42c559da4..25bb44924 100644 --- a/src/client/views/nodes/formattedText/DailyJournal.tsx +++ b/src/client/views/nodes/formattedText/DailyJournal.tsx @@ -56,18 +56,18 @@ export class DailyJournal extends ViewBoxAnnotatableComponent<FieldViewProps>() console.log('Checking if dataDoc has text field...'); - const styles = { - bold: true, // Make the journal date bold + const styles = { + bold: true, // Make the journal date bold color: 'blue', // Set the journal date color to blue - fontSize: 18 // Set the font size to 18px for the whole text + fontSize: 18, // Set the font size to 18px for the whole text }; console.log('Setting new text field with:', initialText); - this.dataDoc[this.fieldKey] = RichTextField.textToRtfFormatting( + this.dataDoc[this.fieldKey] = RichTextField.textToRtf( initialText, undefined, // No image DocId - placeholderText.length, // The position for text selection - styles // Pass the styles object here + styles, // Pass the styles object here + placeholderText.length // The position for text selection ); console.log('Current text field:', this.dataDoc[this.fieldKey]); diff --git a/src/fields/Doc.ts b/src/fields/Doc.ts index 805bb4526..dc4a5a011 100644 --- a/src/fields/Doc.ts +++ b/src/fields/Doc.ts @@ -1797,7 +1797,3 @@ ScriptingGlobals.add(function setDocRangeFilter(container: Doc, key: string, ran ScriptingGlobals.add(function toJavascriptString(str: string) { return Field.toJavascriptString(str as FieldType); }); -// eslint-disable-next-line prefer-arrow-callback -ScriptingGlobals.add(function RtfField() { - return RichTextField.RTFfield(); -}); 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); } - - - - } |