From 2e4b6c5fc5900db40d8e823c35d486e478dd8c09 Mon Sep 17 00:00:00 2001 From: Alexander Onnikov Date: Mon, 4 Dec 2023 13:41:36 +0700 Subject: [PATCH] Fix formatting warnings in text-editor package (#4128) Signed-off-by: Alexander Onnikov --- .../components/CollaborationDiffViewer.svelte | 8 ++--- .../src/components/CollaboratorEditor.svelte | 36 +++++++++---------- .../src/components/ImageStyleToolbar.svelte | 8 ++--- .../src/components/LinkPopup.svelte | 2 +- .../src/components/MarkupDiffViewer.svelte | 10 +++--- .../src/components/ReferenceInput.svelte | 8 ++--- .../src/components/StringDiffViewer.svelte | 2 +- .../src/components/StyledTextArea.svelte | 6 ++-- .../src/components/StyledTextBox.svelte | 17 ++++----- .../src/components/StyledTextEditor.svelte | 2 +- .../src/components/TextEditor.svelte | 14 ++++---- .../components/TextEditorStyleToolbar.svelte | 8 ++--- .../src/components/toc/TableOfContents.svelte | 6 ++-- .../toc/TableOfContentsPopup.svelte | 2 +- 14 files changed, 65 insertions(+), 64 deletions(-) diff --git a/packages/text-editor/src/components/CollaborationDiffViewer.svelte b/packages/text-editor/src/components/CollaborationDiffViewer.svelte index f982de5f1e..b953db4274 100644 --- a/packages/text-editor/src/components/CollaborationDiffViewer.svelte +++ b/packages/text-editor/src/components/CollaborationDiffViewer.svelte @@ -48,8 +48,8 @@ } } - const updateDecorations = () => { - if (editor?.schema && comparedYdoc) { + const updateDecorations = (): void => { + if (editor?.schema !== undefined && comparedYdoc !== undefined) { updateEditor(editor, comparedYdoc, comparedField) } } @@ -70,7 +70,7 @@ } }) - $: if (editor && comparedYdoc) { + $: if (editor !== undefined && comparedYdoc !== undefined) { updateEditor(editor, comparedYdoc, comparedField) } @@ -84,7 +84,7 @@ }) onDestroy(() => { - if (editor) { + if (editor !== undefined) { editor.destroy() } }) diff --git a/packages/text-editor/src/components/CollaboratorEditor.svelte b/packages/text-editor/src/components/CollaboratorEditor.svelte index 5e63825fba..7842463dd1 100644 --- a/packages/text-editor/src/components/CollaboratorEditor.svelte +++ b/packages/text-editor/src/components/CollaboratorEditor.svelte @@ -109,21 +109,21 @@ } export function getHTML (): string | undefined { - if (editor) { + if (editor !== undefined) { return editor.getHTML() } } export function getNodeElement (uuid: string): Element | null { - if (!editor || !uuid) { + if (editor === undefined || uuid === '') { return null } return editor.view.dom.querySelector(nodeElementQuerySelector(uuid)) } - export function selectNode (uuid: string) { - if (!editor) { + export function selectNode (uuid: string): void { + if (editor === undefined) { return } @@ -138,7 +138,7 @@ (mark) => mark.type.name === NodeUuidExtension.name && mark.attrs[NodeUuidExtension.name] === uuid ) - if (!nodeUuidMark) { + if (nodeUuidMark === undefined) { return } @@ -147,7 +147,7 @@ // the first pos does not contain the mark, so we need to add 1 (pos + 1) to get the correct range const range = getMarkRange(doc.resolve(pos + 1), schema.marks[NodeUuidExtension.name]) - if (!range) { + if (range === undefined) { return false } @@ -157,8 +157,8 @@ }) } - export function selectRange (from: number, to: number) { - if (!editor) { + export function selectRange (from: number, to: number): void { + if (editor === undefined) { return } @@ -169,18 +169,18 @@ } export function setNodeUuid (nodeId: string): boolean { - if (!editor || editor.view.state.selection.empty || !nodeId) { + if (editor === undefined || editor.view.state.selection.empty || nodeId === '') { return false } return editor.chain().setNodeUuid(nodeId).run() } - export function takeSnapshot (snapshotId: string) { + export function takeSnapshot (snapshotId: string): void { copyDocumentContent(documentId, snapshotId, { provider }, initialContentId) } - export function copyField (srcFieldId: string, dstFieldId: string) { + export function copyField (srcFieldId: string, dstFieldId: string): void { copyDocumentField(documentId, srcFieldId, dstFieldId, { provider }, initialContentId) } @@ -193,7 +193,7 @@ needFocus = true } - $: if (editor && needFocus) { + $: if (editor !== undefined && needFocus) { if (!focused) { editor.commands.focus(posFocus) posFocus = undefined @@ -236,7 +236,7 @@ } onMount(() => { - ph.then(() => { + void ph.then(() => { editor = new Editor({ element, editable: true, @@ -309,14 +309,14 @@ } }) - if (initialContent) { + if (initialContent !== undefined) { editor.commands.insertContent(initialContent) } }) }) onDestroy(() => { - if (editor) { + if (editor !== undefined) { try { editor.destroy() } catch (err: any) {} @@ -337,16 +337,16 @@ isFocus: () => document.activeElement === element, canBlur: () => false }) - const updateFocus = () => { + const updateFocus = (): void => { if (focusIndex !== -1) { focusManager?.setFocus(idx) } } - $: if (element) { + $: if (element !== undefined) { element.addEventListener('focus', updateFocus, { once: true }) } - function handleFocus () { + function handleFocus (): void { needFocus = true } diff --git a/packages/text-editor/src/components/ImageStyleToolbar.svelte b/packages/text-editor/src/components/ImageStyleToolbar.svelte index fea2091f46..15f91147fa 100644 --- a/packages/text-editor/src/components/ImageStyleToolbar.svelte +++ b/packages/text-editor/src/components/ImageStyleToolbar.svelte @@ -37,7 +37,7 @@ } } - function openImage () { + function openImage (): void { const attributes = textEditor.getAttributes('image') const fileId = attributes['file-id'] ?? attributes.src const fileName = attributes.alt ?? '' @@ -51,14 +51,14 @@ ) } - function openOriginalImage () { + function openOriginalImage (): void { const attributes = textEditor.getAttributes('image') const fileId = attributes['file-id'] ?? attributes.src const url = getFileUrl(fileId, 'full') window.open(url, '_blank') } - function moreOptions (event: MouseEvent) { + function moreOptions (event: MouseEvent): void { const widthActions = ['25%', '50%', '75%', '100%', plugin.string.Unset].map((it) => { return { id: `#imageWidth${it}`, @@ -81,7 +81,7 @@ (val) => { if (val !== undefined) { const op = actions.find((it) => it.id === val) - if (op) { + if (op !== undefined) { op.action() dispatch('focus') } diff --git a/packages/text-editor/src/components/LinkPopup.svelte b/packages/text-editor/src/components/LinkPopup.svelte index f7f89315ff..6f7ed155fc 100644 --- a/packages/text-editor/src/components/LinkPopup.svelte +++ b/packages/text-editor/src/components/LinkPopup.svelte @@ -26,7 +26,7 @@ const dispatch = createEventDispatcher() const linkPlaceholder = getEmbeddedLabel('http://my.link.net') - function save () { + function save (): void { dispatch('update', link) } diff --git a/packages/text-editor/src/components/MarkupDiffViewer.svelte b/packages/text-editor/src/components/MarkupDiffViewer.svelte index 8d8e107f2c..c75efb0f11 100644 --- a/packages/text-editor/src/components/MarkupDiffViewer.svelte +++ b/packages/text-editor/src/components/MarkupDiffViewer.svelte @@ -35,7 +35,7 @@ let oldContent = '' function updateEditor (editor: Editor, comparedVersion?: Markup): void { - if (!comparedVersion) { + if (comparedVersion === undefined) { return } @@ -46,8 +46,8 @@ } } - const updateDecorations = () => { - if (editor?.schema) { + const updateDecorations = (): void => { + if (editor?.schema !== undefined) { updateEditor(editor, comparedVersion) } } @@ -68,7 +68,7 @@ } }) - $: if (editor && comparedVersion) { + $: if (editor !== undefined && comparedVersion !== undefined) { updateEditor(editor, comparedVersion) } @@ -87,7 +87,7 @@ }) onDestroy(() => { - if (editor) { + if (editor !== undefined) { editor.destroy() } }) diff --git a/packages/text-editor/src/components/ReferenceInput.svelte b/packages/text-editor/src/components/ReferenceInput.svelte index 14e6c57b6d..4aca118f42 100644 --- a/packages/text-editor/src/components/ReferenceInput.svelte +++ b/packages/text-editor/src/components/ReferenceInput.svelte @@ -61,7 +61,7 @@ $: devSize = $deviceInfo.size $: shrinkButtons = checkAdaptiveMatching(devSize, 'sm') - function setContent (content: string) { + function setContent (content: string): void { textEditor?.setContent(content) } @@ -106,7 +106,7 @@ export let focusIndex = -1 const { idx, focusManager } = registerFocus(focusIndex, { focus: () => { - const editable = textEditor?.isEditable() ?? false + const editable: boolean = textEditor?.isEditable() ?? false if (editable) { focused = true textEditor?.focus() @@ -115,7 +115,7 @@ }, isFocus: () => focused }) - const updateFocus = () => { + const updateFocus = (): void => { if (focusIndex !== -1) { focusManager?.setFocus(idx) } @@ -184,7 +184,7 @@ showTooltip={{ label: a.label }} size={buttonSize} on:click={handler(a, (a, evt) => { - if (!a.disabled) { + if (a.disabled !== true) { handleAction(a, evt) } })} diff --git a/packages/text-editor/src/components/StringDiffViewer.svelte b/packages/text-editor/src/components/StringDiffViewer.svelte index e8406f8428..45803090a4 100644 --- a/packages/text-editor/src/components/StringDiffViewer.svelte +++ b/packages/text-editor/src/components/StringDiffViewer.svelte @@ -21,7 +21,7 @@ export let compareTo: string export let method: 'diffChars' | 'diffWords' | 'diffWordsWithSpace' = 'diffChars' - const handleDiff = (oldValue: string, newValue: string) => Diff[method](oldValue, newValue) + const handleDiff = (oldValue: string, newValue: string): Diff.Change[] => Diff[method](oldValue, newValue) $: changes = handleDiff(compareTo, value) diff --git a/packages/text-editor/src/components/StyledTextArea.svelte b/packages/text-editor/src/components/StyledTextArea.svelte index 79b33a8dbd..b75df3c8d8 100644 --- a/packages/text-editor/src/components/StyledTextArea.svelte +++ b/packages/text-editor/src/components/StyledTextArea.svelte @@ -24,7 +24,7 @@ let rawValue: string let oldContent = '' - $: if (content && oldContent !== content) { + $: if (content !== undefined && oldContent !== content) { oldContent = content rawValue = content } @@ -44,12 +44,12 @@ let needFocus = focus - $: if (textEditor && needFocus) { + $: if (textEditor !== undefined && needFocus) { textEditor.focus() needFocus = false } - function configureExtensions () { + function configureExtensions (): AnyExtension[] { const completionPlugin = Completion.configure({ ...completionConfig, showDoc (event: MouseEvent, _id: string, _class: string) { diff --git a/packages/text-editor/src/components/StyledTextBox.svelte b/packages/text-editor/src/components/StyledTextBox.svelte index ffc8024ef4..7e6db7032c 100644 --- a/packages/text-editor/src/components/StyledTextBox.svelte +++ b/packages/text-editor/src/components/StyledTextBox.svelte @@ -69,12 +69,16 @@ mode = Mode.View } + const dispatch = createEventDispatcher() + let canBlur = true let focused = false let rawValue: string let oldContent = '' let modified: boolean = false + let textEditor: StyledTextEditor + $: if (oldContent !== content) { oldContent = content if (rawValue !== content) { @@ -86,8 +90,6 @@ $: if (!modified && rawValue !== content) modified = true $: dispatch('change', modified) - let textEditor: StyledTextEditor - export function submit (): void { textEditor.submit() } @@ -103,14 +105,13 @@ export function setContent (data: string): void { textEditor.setContent(data) } - const dispatch = createEventDispatcher() export function isFocused (): boolean { return focused } let needFocus = false - $: if (textEditor && needFocus) { + $: if (textEditor !== undefined && needFocus) { textEditor.focus() needFocus = false } @@ -119,7 +120,7 @@ export let focusIndex = -1 const { idx, focusManager } = registerFocus(focusIndex, { focus: () => { - const editable = textEditor?.isEditable() + const editable = textEditor?.isEditable() ?? false if (editable) { focused = true focus() @@ -134,13 +135,13 @@ return true } }) - const updateFocus = () => { + const updateFocus = (): void => { if (focusIndex !== -1) { focusManager?.setFocus(idx) } } - const handleFocus = (value: boolean) => { + const handleFocus = (value: boolean): void => { focused = value if (focused) { updateFocus() @@ -166,7 +167,7 @@ } } - function configureExtensions () { + function configureExtensions (): AnyExtension[] { const imagePlugin = ImageExtension.configure({ inline: true, HTMLAttributes: {}, diff --git a/packages/text-editor/src/components/StyledTextEditor.svelte b/packages/text-editor/src/components/StyledTextEditor.svelte index 10185f993e..2b929970c0 100644 --- a/packages/text-editor/src/components/StyledTextEditor.svelte +++ b/packages/text-editor/src/components/StyledTextEditor.svelte @@ -125,7 +125,7 @@ let needFocus = autofocus const focused = false - $: if (textEditor && needFocus) { + $: if (textEditor !== undefined && needFocus) { if (!focused) textEditor.focus() needFocus = false } diff --git a/packages/text-editor/src/components/TextEditor.svelte b/packages/text-editor/src/components/TextEditor.svelte index 70a13f7f01..7d6e07f7b5 100644 --- a/packages/text-editor/src/components/TextEditor.svelte +++ b/packages/text-editor/src/components/TextEditor.svelte @@ -55,7 +55,9 @@ return editor.isEditable } export function setEditable (editable: boolean): void { - if (editor) editor.setEditable(editable) + if (editor !== undefined) { + editor.setEditable(editable) + } } export function submit (): void { content = editor.getHTML() @@ -104,7 +106,7 @@ needFocus = true } - $: if (editor && needFocus) { + $: if (editor != null && needFocus) { if (!focused) { editor.commands.focus(posFocus) posFocus = undefined @@ -146,7 +148,7 @@ }) onMount(() => { - ph.then(() => { + void ph.then(() => { editor = new Editor({ element, editorProps: { attributes: mergeAttributes(defaultEditorAttributes, editorAttributes) }, @@ -197,16 +199,14 @@ }) onDestroy(() => { - if (editor) { - editor.destroy() - } + editor?.destroy() }) /** * @public */ export function removeNode (nde: ProseMirrorNode): void { - const deleteOp = (n: ProseMirrorNode, pos: number) => { + const deleteOp = (n: ProseMirrorNode, pos: number): void => { if (nde === n) { // const pos = editor.view.posAtDOM(nde, 0) editor.view.dispatch(editor.view.state.tr.delete(pos, pos + 1)) diff --git a/packages/text-editor/src/components/TextEditorStyleToolbar.svelte b/packages/text-editor/src/components/TextEditorStyleToolbar.svelte index 45dd974ef8..3309ab9dab 100644 --- a/packages/text-editor/src/components/TextEditorStyleToolbar.svelte +++ b/packages/text-editor/src/components/TextEditorStyleToolbar.svelte @@ -79,7 +79,7 @@ } } - function insertTable (event: MouseEvent) { + function insertTable (event: MouseEvent): void { showPopup( SelectPopup, { @@ -97,7 +97,7 @@ return } const tab = mInsertTable.find((it) => it.label === val) - if (tab) { + if (tab !== undefined) { textEditor.commands.insertTable({ cols: tab.cols, rows: tab.rows, @@ -111,7 +111,7 @@ ) } - function tableOptions (event: MouseEvent) { + function tableOptions (event: MouseEvent): void { const ops = [ { id: '#addColumnBefore', @@ -188,7 +188,7 @@ (val) => { if (val !== undefined) { const op = ops.find((it) => it.id === val) - if (op) { + if (op !== undefined) { op.action() dispatch('focus') } diff --git a/packages/text-editor/src/components/toc/TableOfContents.svelte b/packages/text-editor/src/components/toc/TableOfContents.svelte index 19439798e3..ed68ead5e3 100644 --- a/packages/text-editor/src/components/toc/TableOfContents.svelte +++ b/packages/text-editor/src/components/toc/TableOfContents.svelte @@ -28,13 +28,13 @@ $: minLevel = items.reduce((p, v) => Math.min(p, v.level), Infinity) $: maxLevel = items.reduce((p, v) => Math.max(p, v.level), 0) - function getLevelWidth (level: number) { + function getLevelWidth (level: number): number { return (100 * (maxLevel - level + 1)) / (maxLevel - minLevel + 1) } let hovered = false - function handleOpenToc (ev: MouseEvent) { + function handleOpenToc (ev: MouseEvent): void { ev.preventDefault() ev.stopPropagation() @@ -46,7 +46,7 @@ getPopupPositionElement(ev.target as HTMLElement, { v: 'top', h: 'right' }), (res) => { hovered = false - if (res) { + if (res != null) { dispatch('select', res) } } diff --git a/packages/text-editor/src/components/toc/TableOfContentsPopup.svelte b/packages/text-editor/src/components/toc/TableOfContentsPopup.svelte index 249bbe2dbe..0cd0fd510c 100644 --- a/packages/text-editor/src/components/toc/TableOfContentsPopup.svelte +++ b/packages/text-editor/src/components/toc/TableOfContentsPopup.svelte @@ -25,7 +25,7 @@ $: minLevel = items.reduce((p, v) => Math.min(p, v.level), Infinity) - function getIndentLevel (level: number) { + function getIndentLevel (level: number): number { return 1 * (level - minLevel) }