diff options
author | eeng5 <eleanor_eng@brown.edu> | 2020-02-09 14:59:57 -0500 |
---|---|---|
committer | eeng5 <eleanor_eng@brown.edu> | 2020-02-09 14:59:57 -0500 |
commit | daa8898f70bd44d2716f1f0d3371a8435a3ff638 (patch) | |
tree | ccb2ea544eeed0413370cdd69e758faec6170690 /src/new_fields/RichTextField.ts | |
parent | 1e36055e3674fe33dd39f6ad79c969531f7b447c (diff) | |
parent | f6179334d6f2942631caa17b7c8ae2531d87c7c4 (diff) |
Merge branch 'pen' of https://github.com/browngraphicslab/Dash-Web into pen
Diffstat (limited to 'src/new_fields/RichTextField.ts')
-rw-r--r-- | src/new_fields/RichTextField.ts | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/new_fields/RichTextField.ts b/src/new_fields/RichTextField.ts index a0f21f45e..7b6ce9b98 100644 --- a/src/new_fields/RichTextField.ts +++ b/src/new_fields/RichTextField.ts @@ -1,9 +1,12 @@ import { ObjectField } from "./ObjectField"; import { serializable } from "serializr"; import { Deserializable } from "../client/util/SerializationHelper"; -import { Copy, ToScriptString, ToString } from "./FieldSymbols"; +import { Copy, ToScriptString, ToPlainText, ToString } from "./FieldSymbols"; import { scriptingGlobal } from "../client/util/Scripting"; +const delimiter = "\n"; +const joiner = ""; + @scriptingGlobal @Deserializable("RichTextField") export class RichTextField extends ObjectField { @@ -30,4 +33,20 @@ export class RichTextField extends ObjectField { return this.Text; } + [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); + } + }
\ No newline at end of file |