aboutsummaryrefslogtreecommitdiff
path: root/src/fields/RichTextUtils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/fields/RichTextUtils.ts')
-rw-r--r--src/fields/RichTextUtils.ts60
1 files changed, 33 insertions, 27 deletions
diff --git a/src/fields/RichTextUtils.ts b/src/fields/RichTextUtils.ts
index 3763dcd2c..b3534dde7 100644
--- a/src/fields/RichTextUtils.ts
+++ b/src/fields/RichTextUtils.ts
@@ -1,9 +1,10 @@
+/* eslint-disable @typescript-eslint/no-namespace */
/* eslint-disable no-await-in-loop */
/* eslint-disable no-use-before-define */
import { AssertionError } from 'assert';
import * as Color from 'color';
import { docs_v1 as docsV1 } from 'googleapis';
-import { Fragment, Mark, Node } from 'prosemirror-model';
+import { Fragment, Mark, Node, Schema } from 'prosemirror-model';
import { sinkListItem } from 'prosemirror-schema-list';
import { EditorState, TextSelection, Transaction } from 'prosemirror-state';
import { ClientUtils, DashColor } from '../ClientUtils';
@@ -26,7 +27,7 @@ export namespace RichTextUtils {
const joiner = '';
export const Initialize = (initial?: string) => {
- const content: any[] = [];
+ const content: object[] = [];
const state = {
doc: {
type: 'doc',
@@ -80,8 +81,10 @@ export namespace RichTextUtils {
// Preserve the current state, but re-write the content to be the blocks
const parsed = JSON.parse(oldState ? oldState.Data : Initialize());
parsed.doc.content = elements.map(text => {
- const paragraph: any = { type: 'paragraph' };
- text.length && (paragraph.content = [{ type: 'text', marks: [], text }]); // An empty paragraph gets treated as a line break
+ const paragraph: object = {
+ type: 'paragraph',
+ content: text.length ? [{ type: 'text', marks: [], text }] : undefined, // An empty paragraph gets treated as a line break
+ };
return paragraph;
});
@@ -164,7 +167,7 @@ export namespace RichTextUtils {
const inlineObjectMap = await parseInlineObjects(document);
const title = document.title!;
const { text, paragraphs } = GoogleApiClientUtils.Docs.Utils.extractText(document);
- let state = EditorState.create(new FormattedTextBox({} as any).config);
+ let state = EditorState.create(FormattedTextBox.MakeConfig());
const structured = parseLists(paragraphs);
let position = 3;
@@ -253,17 +256,20 @@ export namespace RichTextUtils {
return groups;
};
- const listItem = (lschema: any, runs: docsV1.Schema$TextRun[]): Node => lschema.node('list_item', null, paragraphNode(lschema, runs));
+ const listItem = (lschema: Schema, runs: docsV1.Schema$TextRun[]): Node => lschema.node('list_item', null, paragraphNode(lschema, runs));
- const list = (lschema: any, items: Node[]): Node => lschema.node('ordered_list', { mapStyle: 'bullet' }, items);
+ const list = (lschema: Schema, items: Node[]): Node => lschema.node('ordered_list', { mapStyle: 'bullet' }, items);
- const paragraphNode = (lschema: any, runs: docsV1.Schema$TextRun[]): Node => {
- const children = runs.map(run => textNode(lschema, run)).filter(child => child !== undefined);
+ const paragraphNode = (lschema: Schema, runs: docsV1.Schema$TextRun[]): Node => {
+ const children = runs
+ .map(run => textNode(lschema, run))
+ .filter(child => child !== undefined)
+ .map(child => child!);
const fragment = children.length ? Fragment.from(children) : undefined;
return lschema.node('paragraph', null, fragment);
};
- const imageNode = (lschema: any, image: ImageTemplate, textNote: Doc) => {
+ const imageNode = (lschema: Schema, image: ImageTemplate, textNote: Doc) => {
const { url: src, width, agnostic } = image;
let docId: string;
const guid = Utils.GenerateDeterministicGuid(agnostic);
@@ -279,7 +285,7 @@ export namespace RichTextUtils {
return lschema.node('image', { src, agnostic, width, docId, float: null });
};
- const textNode = (lschema: any, run: docsV1.Schema$TextRun) => {
+ const textNode = (lschema: Schema, run: docsV1.Schema$TextRun) => {
const text = run.content!.removeTrailingNewlines();
return text.length ? lschema.text(text, styleToMarks(lschema, run.textStyle)) : undefined;
};
@@ -291,29 +297,33 @@ export namespace RichTextUtils {
['fontSize', 'pFontSize'],
]);
- const styleToMarks = (lschema: any, textStyle?: docsV1.Schema$TextStyle) => {
+ const styleToMarks = (lschema: Schema, textStyle?: docsV1.Schema$TextStyle) => {
if (!textStyle) {
return undefined;
}
const marks: Mark[] = [];
Object.keys(textStyle).forEach(key => {
const targeted = key as keyof docsV1.Schema$TextStyle;
- const value = textStyle[targeted] as any;
+ const value = textStyle[targeted];
if (value) {
- const attributes: any = {};
+ const attributes: { [key: string]: number | string } = {};
let converted = StyleToMark.get(targeted) || targeted;
- value.url && (attributes.href = value.url);
- if (value.color) {
- const object = value.color.rgbColor;
- attributes.color = Color.rgb(['red', 'green', 'blue'].map(color => object[color] * 255 || 0)).hex();
+ const urlValue = value as docsV1.Schema$Link;
+ urlValue.url && (attributes.href = urlValue.url);
+ const colValue = value as docsV1.Schema$OptionalColor;
+ const object = colValue.color?.rgbColor;
+ if (object) {
+ attributes.color = Color.rgb(['red', 'green', 'blue'].map(color => (object as { [key: string]: number })[color] * 255 || 0)).hex();
}
- if (value.magnitude) {
- attributes.fontSize = value.magnitude;
+ const magValue = value as docsV1.Schema$Dimension;
+ if (magValue.magnitude) {
+ attributes.fontSize = magValue.magnitude;
}
+ const fontValue = value as docsV1.Schema$WeightedFontFamily;
if (converted === 'weightedFontFamily') {
- converted = ImportFontFamilyMapping.get(value.fontFamily) || 'timesNewRoman';
+ converted = (fontValue.fontFamily && ImportFontFamilyMapping.get(fontValue.fontFamily)) || 'timesNewRoman';
}
const mapped = lschema.marks[converted];
@@ -384,13 +394,11 @@ export namespace RichTextUtils {
for (const markName of Object.keys(schema.marks)) {
// eslint-disable-next-line no-cond-assign
if (ignored.includes(markName) || !(mark = markMap[markName])) {
- // eslint-disable-next-line no-continue
continue;
}
let converted = MarkToStyle.get(markName) || (markName as keyof docsV1.Schema$TextStyle);
- let value: any = true;
+ let value: unknown = true;
if (!converted) {
- // eslint-disable-next-line no-continue
continue;
}
// eslint-disable-next-line @typescript-eslint/no-shadow
@@ -402,10 +410,8 @@ export namespace RichTextUtils {
const docDelimeter = '/doc/';
const alreadyShared = '?sharing=true';
if (new RegExp(window.location.origin + docDelimeter).test(url) && !url.endsWith(alreadyShared)) {
- // eslint-disable-next-line no-await-in-loop
const linkDoc = await DocServer.GetRefField(url.split(docDelimeter)[1]);
if (linkDoc instanceof Doc) {
- // eslint-disable-next-line no-await-in-loop
let exported = (await Cast(linkDoc.link_anchor_2, Doc))!;
if (!exported.customLayout) {
exported = Doc.MakeEmbedding(exported);
@@ -436,7 +442,7 @@ export namespace RichTextUtils {
converted = 'fontSize';
value = { magnitude: parseInt(matches[1].replace('px', '')), unit: 'PT' };
}
- textStyle[converted] = value;
+ textStyle[converted] = value as undefined;
}
if (Object.keys(textStyle).length) {
requests.push(EncodeStyleUpdate(information));