Skip to content

Commit 3c187fb

Browse files
committed
chore(type): EditorFactory and setInitialYjsState
Signed-off-by: Max <max@nextcloud.com>
1 parent f96416f commit 3c187fb

10 files changed

Lines changed: 31 additions & 17 deletions

File tree

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@
55

66
import 'proxy-polyfill'
77

8-
import { Editor } from '@tiptap/core'
8+
import { Editor, Extension } from '@tiptap/core'
99
import hljs from 'highlight.js/lib/core'
1010
import { createLowlight } from 'lowlight'
1111

12+
import type { Node } from '@tiptap/pm/model'
13+
import type { Connection } from './composables/useConnection'
1214
import { FocusTrap, PlainText, RichText } from './extensions/index.js'
1315
import { logger } from './helpers/logger.ts'
1416

1517
const lowlight = createLowlight()
1618

17-
const loadSyntaxHighlight = async (language) => {
19+
const loadSyntaxHighlight = async (language: string) => {
1820
const list = hljs.listLanguages()
1921
logger.debug('Supported languages', { list })
2022
if (!lowlight.listLanguages().includes(language)) {
2123
try {
22-
logger.debug('Loading language', language)
24+
logger.debug('Loading language ' + language)
2325
// eslint-disable-next-line n/no-missing-import
2426
const syntax = await import(
2527
`../node_modules/highlight.js/lib/languages/${language}.js`
@@ -42,6 +44,11 @@ const createRichEditor = ({
4244
connection,
4345
relativePath,
4446
isEmbedded = false,
47+
}: {
48+
extensions?: Extension[]
49+
connection?: Connection
50+
relativePath?: string
51+
isEmbedded?: boolean
4552
} = {}) => {
4653
return new Editor({
4754
editorProps,
@@ -53,7 +60,10 @@ const createRichEditor = ({
5360
})
5461
}
5562

56-
const createPlainEditor = ({ language = 'plaintext', extensions = [] } = {}) => {
63+
const createPlainEditor = ({
64+
language = 'plaintext',
65+
extensions = [],
66+
}: { language?: string; extensions?: Extension[] } = {}) => {
5767
return new Editor({
5868
editorProps,
5969
extensions: [
@@ -68,7 +78,7 @@ const createPlainEditor = ({ language = 'plaintext', extensions = [] } = {}) =>
6878
})
6979
}
7080

71-
const serializePlainText = (doc) => {
81+
const serializePlainText = (doc: Node) => {
7282
return doc.textContent
7383
}
7484

src/components/Editor.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ import { useSyntaxHighlighting } from '../composables/useSyntaxHighlighting.ts'
108108
import { CollaborationCaret } from '../extensions/index.js'
109109
import { exposeForDebugging, removeFromDebugging } from '../helpers/debug.ts'
110110
import { logger } from '../helpers/logger.ts'
111-
import { setInitialYjsState } from '../helpers/setInitialYjsState.js'
111+
import { setInitialYjsState } from '../helpers/setInitialYjsState.ts'
112112
import { ERROR_TYPE, IDLE_TIMEOUT } from '../services/SyncService.ts'
113113
import { fetchNode } from '../services/WebdavClient.ts'
114114
import {
115115
createPlainEditor,
116116
createRichEditor,
117117
serializePlainText,
118-
} from './../EditorFactory.js'
118+
} from './../EditorFactory.ts'
119119
import { createMarkdownSerializer } from './../extensions/Markdown.js'
120120
import markdownit from './../markdownit/index.js'
121121
import isMobile from './../mixins/isMobile.js'
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { applyUpdate, Doc, encodeStateAsUpdate, XmlFragment } from 'yjs'
1010
import { createPlainEditor, createRichEditor } from '../EditorFactory.js'
1111
import markdownit from '../markdownit/index.js'
1212

13-
export const setInitialYjsState = (ydoc, content, { isRichEditor }) => {
13+
export const setInitialYjsState = (
14+
ydoc: Doc,
15+
content: string,
16+
{ isRichEditor }: { isRichEditor: boolean },
17+
) => {
1418
const html = isRichEditor
1519
? markdownit.render(content) + '<p/>'
1620
: `<pre>${escapeHtml(content)}</pre>`
@@ -21,7 +25,7 @@ export const setInitialYjsState = (ydoc, content, { isRichEditor }) => {
2125
const json = editor.getJSON()
2226

2327
const node = Node.fromJSON(editor.schema, json)
24-
const getBaseDoc = (node) => {
28+
const getBaseDoc = (node: Node) => {
2529
const baseDoc = new Doc()
2630
// In order to make the initial document state idempotent, we need to reset the clientID
2731
// While this is not recommended, we cannot avoid it here as we lack another mechanism
@@ -30,7 +34,7 @@ export const setInitialYjsState = (ydoc, content, { isRichEditor }) => {
3034
// it to the server immediately, however this would require read only sessions to be able
3135
// to still push a state
3236
baseDoc.clientID = 0
33-
const type = /** @type {XmlFragment} */ (baseDoc.get('default', XmlFragment))
37+
const type = /** @type {XmlFragment} */ baseDoc.get('default', XmlFragment)
3438
if (!type.doc) {
3539
// This should not happen but is aligned with the upstream implementation
3640
// https://github.com/yjs/y-prosemirror/blob/8db24263770c2baaccb08e08ea9ef92dbcf8a9da/src/lib.js#L209

src/tests/markdown.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
import { createRichEditor } from '../EditorFactory.js'
6+
import { createRichEditor } from '../EditorFactory.ts'
77
import { createMarkdownSerializer } from '../extensions/Markdown.js'
88
import { typesAvailable } from './../markdownit/callouts.js'
99
import markdownit from './../markdownit/index.js'

src/tests/nodes/Table.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { test as baseTest } from 'vitest'
7-
import { createRichEditor } from '../../EditorFactory.js'
7+
import { createRichEditor } from '../../EditorFactory.ts'
88
import { createMarkdownSerializer } from '../../extensions/Markdown.js'
99
import { markdownThroughEditor } from '../testHelpers/markdown.js'
1010

src/tests/plaintext.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
import { createPlainEditor, serializePlainText } from './../EditorFactory.js'
6+
import { createPlainEditor, serializePlainText } from './../EditorFactory.ts'
77
import spec from './fixtures/spec.js'
88
import xssFuzzVectors from './fixtures/xssFuzzVectors.js'
99

src/tests/testHelpers/builders.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { Node } from '@tiptap/pm/model'
77
import { builders } from 'prosemirror-test-builder'
88
import { expect } from 'vitest'
9-
import { createRichEditor } from '../../EditorFactory.js'
9+
import { createRichEditor } from '../../EditorFactory.ts'
1010

1111
/**
1212
* Get node builders from the default rich editor.

src/tests/testHelpers/markdown.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
import { createRichEditor } from '../../EditorFactory.js'
6+
import { createRichEditor } from '../../EditorFactory.ts'
77
import { createMarkdownSerializer } from '../../extensions/Markdown.js'
88
import markdownit from '../../markdownit/index.js'
99

src/tests/tiptap.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
import { createRichEditor } from '../EditorFactory.js'
6+
import { createRichEditor } from '../EditorFactory.ts'
77
import markdownit from '../markdownit/index.js'
88

99
const renderedHTML = (markdown) => {

src/tests/yjs.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as decoding from 'lib0/decoding'
88
import * as encoding from 'lib0/encoding'
99
import * as syncProtocol from 'y-protocols/sync'
1010
import { Doc, encodeStateAsUpdate } from 'yjs'
11-
import { createRichEditor } from '../EditorFactory.js'
11+
import { createRichEditor } from '../EditorFactory.ts'
1212
import { createMarkdownSerializer } from '../extensions/Markdown.js'
1313
import { decodeArrayBuffer } from '../helpers/base64.ts'
1414
import recorded from './fixtures/recorded.js'

0 commit comments

Comments
 (0)