From 4721750d4e8d6ac18a3155f2570a48007a38217f Mon Sep 17 00:00:00 2001 From: jocs Date: Mon, 22 Apr 2024 23:15:58 +0800 Subject: [PATCH] refactor: use Nullable to replace --- packages/core/src/block/base/format.ts | 5 +++-- .../base/linkedList/__tests__/linkedlist.spec.ts | 2 +- .../core/src/block/base/linkedList/linkedList.ts | 4 +--- packages/core/src/block/base/parent.ts | 2 +- packages/core/src/block/base/treeNode.ts | 4 ++-- .../core/src/block/commonMark/bulletList/index.ts | 2 +- .../core/src/block/commonMark/listItem/index.ts | 2 +- .../core/src/block/commonMark/orderList/index.ts | 2 +- packages/core/src/block/gfm/table/cell.ts | 2 +- packages/core/src/block/gfm/table/index.ts | 2 +- packages/core/src/block/gfm/table/row.ts | 2 +- packages/core/src/block/gfm/table/table.ts | 2 +- packages/core/src/block/gfm/taskListItem/index.ts | 2 +- packages/core/src/selection/index.ts | 13 +++++-------- 14 files changed, 21 insertions(+), 25 deletions(-) diff --git a/packages/core/src/block/base/format.ts b/packages/core/src/block/base/format.ts index 0d882d9..f9f8b41 100644 --- a/packages/core/src/block/base/format.ts +++ b/packages/core/src/block/base/format.ts @@ -26,6 +26,7 @@ import logger from '../../utils/logger'; import type AtxHeading from '../commonMark/atxHeading'; import type BulletList from '../commonMark/bulletList'; import type SetextHeading from '../commonMark/setextHeading'; +import type { Nullable } from '../../types'; import type Parent from './parent'; interface IOffset { @@ -228,7 +229,7 @@ class Format extends Content { text: string, offset: number, type: Token['type'], - ): Token | null { + ): Nullable { const tokens = tokenizer(text, { hasBeginRules: false, options: this.muya.options, @@ -436,7 +437,7 @@ class Format extends Content { const selector = `#${imageId.includes('_') ? imageId : `${imageId}_${token.range.start}` } img`; - const image: HTMLImageElement | null = document.querySelector(selector); + const image: Nullable = document.querySelector(selector); if (image) image.click(); diff --git a/packages/core/src/block/base/linkedList/__tests__/linkedlist.spec.ts b/packages/core/src/block/base/linkedList/__tests__/linkedlist.spec.ts index 5301426..8a088a4 100644 --- a/packages/core/src/block/base/linkedList/__tests__/linkedlist.spec.ts +++ b/packages/core/src/block/base/linkedList/__tests__/linkedlist.spec.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; -import LinkedList from '../linkedList'; +import { LinkedList } from '../linkedList'; import type { ILinkedNode } from '../linkedNode'; import type { Nullable } from '../../../../types'; diff --git a/packages/core/src/block/base/linkedList/linkedList.ts b/packages/core/src/block/base/linkedList/linkedList.ts index 1545b23..4720e86 100644 --- a/packages/core/src/block/base/linkedList/linkedList.ts +++ b/packages/core/src/block/base/linkedList/linkedList.ts @@ -1,7 +1,7 @@ import type { Nullable } from '../../../types'; import type { ILinkedNode } from './linkedNode'; -class LinkedList { +export class LinkedList { head: Nullable = null; tail: Nullable = null; @@ -118,5 +118,3 @@ class LinkedList { return [...this.iterator()].reduce(callback, memo); } } - -export default LinkedList; diff --git a/packages/core/src/block/base/parent.ts b/packages/core/src/block/base/parent.ts index be888b8..644ac02 100644 --- a/packages/core/src/block/base/parent.ts +++ b/packages/core/src/block/base/parent.ts @@ -1,4 +1,4 @@ -import LinkedList from '../../block/base/linkedList/linkedList'; +import { LinkedList } from '../../block/base/linkedList/linkedList'; import TreeNode from '../../block/base/treeNode'; import { CLASS_NAMES } from '../../config'; import type { TState } from '../../state/types'; diff --git a/packages/core/src/block/base/treeNode.ts b/packages/core/src/block/base/treeNode.ts index e7a8b77..571e938 100644 --- a/packages/core/src/block/base/treeNode.ts +++ b/packages/core/src/block/base/treeNode.ts @@ -174,7 +174,7 @@ class TreeNode implements ILinkedNode { * Find the closest block which blockName is `blockName`. return `null` if not found. * @param {string} blockName */ - closestBlock(blockName: string): TreeNode | null { + closestBlock(blockName: string): Nullable { if (this.blockName === blockName) return this; @@ -190,7 +190,7 @@ class TreeNode implements ILinkedNode { return null; } - farthestBlock(blockName: string): TreeNode | null { + farthestBlock(blockName: string): Nullable { const results: TreeNode[] = []; if (this.blockName === blockName) results.push(this); diff --git a/packages/core/src/block/commonMark/bulletList/index.ts b/packages/core/src/block/commonMark/bulletList/index.ts index 0aa7846..77647df 100644 --- a/packages/core/src/block/commonMark/bulletList/index.ts +++ b/packages/core/src/block/commonMark/bulletList/index.ts @@ -1,4 +1,4 @@ -import LinkedList from '../../base/linkedList/linkedList'; +import { LinkedList } from '../../base/linkedList/linkedList'; import Parent from '../../base/parent'; import IContainerQueryBlock from '../../mixins/containerQueryBlock'; import { ScrollPage } from '../../scrollPage'; diff --git a/packages/core/src/block/commonMark/listItem/index.ts b/packages/core/src/block/commonMark/listItem/index.ts index 25d0787..ae60f48 100644 --- a/packages/core/src/block/commonMark/listItem/index.ts +++ b/packages/core/src/block/commonMark/listItem/index.ts @@ -1,4 +1,4 @@ -import LinkedList from '../../base/linkedList/linkedList'; +import { LinkedList } from '../../base/linkedList/linkedList'; import Parent from '../../base/parent'; import IContainerQueryBlock from '../../mixins/containerQueryBlock'; import { ScrollPage } from '../../scrollPage'; diff --git a/packages/core/src/block/commonMark/orderList/index.ts b/packages/core/src/block/commonMark/orderList/index.ts index ceb0bb7..f9cc611 100644 --- a/packages/core/src/block/commonMark/orderList/index.ts +++ b/packages/core/src/block/commonMark/orderList/index.ts @@ -1,4 +1,4 @@ -import LinkedList from '../../base/linkedList/linkedList'; +import { LinkedList } from '../../base/linkedList/linkedList'; import Parent from '../../base/parent'; import IContainerQueryBlock from '../../mixins/containerQueryBlock'; import { ScrollPage } from '../../scrollPage'; diff --git a/packages/core/src/block/gfm/table/cell.ts b/packages/core/src/block/gfm/table/cell.ts index 6a9eb86..65a59fa 100644 --- a/packages/core/src/block/gfm/table/cell.ts +++ b/packages/core/src/block/gfm/table/cell.ts @@ -1,4 +1,4 @@ -import LinkedList from '../../base/linkedList/linkedList'; +import { LinkedList } from '../../base/linkedList/linkedList'; import Parent from '../../base/parent'; import type TableCellContent from '../../content/tableCell'; import LeafQueryBlock from '../../mixins/leafQueryBlock'; diff --git a/packages/core/src/block/gfm/table/index.ts b/packages/core/src/block/gfm/table/index.ts index 1b9335f..df47805 100644 --- a/packages/core/src/block/gfm/table/index.ts +++ b/packages/core/src/block/gfm/table/index.ts @@ -1,5 +1,5 @@ import diff from 'fast-diff'; -import LinkedList from '../../base/linkedList/linkedList'; +import { LinkedList } from '../../base/linkedList/linkedList'; import Parent from '../../base/parent'; import type TableCellContent from '../../content/tableCell'; import { ScrollPage } from '../../scrollPage'; diff --git a/packages/core/src/block/gfm/table/row.ts b/packages/core/src/block/gfm/table/row.ts index d57f08e..f108932 100644 --- a/packages/core/src/block/gfm/table/row.ts +++ b/packages/core/src/block/gfm/table/row.ts @@ -1,4 +1,4 @@ -import LinkedList from '../../base/linkedList/linkedList'; +import { LinkedList } from '../../base/linkedList/linkedList'; import Parent from '../../base/parent'; import IContainerQueryBlock from '../../mixins/containerQueryBlock'; import { ScrollPage } from '../../scrollPage'; diff --git a/packages/core/src/block/gfm/table/table.ts b/packages/core/src/block/gfm/table/table.ts index a648e24..4926035 100644 --- a/packages/core/src/block/gfm/table/table.ts +++ b/packages/core/src/block/gfm/table/table.ts @@ -1,4 +1,4 @@ -import LinkedList from '../../base/linkedList/linkedList'; +import { LinkedList } from '../../base/linkedList/linkedList'; import Parent from '../../base/parent'; import IContainerQueryBlock from '../../mixins/containerQueryBlock'; import { ScrollPage } from '../../scrollPage'; diff --git a/packages/core/src/block/gfm/taskListItem/index.ts b/packages/core/src/block/gfm/taskListItem/index.ts index 93974a9..db1b53a 100644 --- a/packages/core/src/block/gfm/taskListItem/index.ts +++ b/packages/core/src/block/gfm/taskListItem/index.ts @@ -1,4 +1,4 @@ -import LinkedList from '../../base/linkedList/linkedList'; +import { LinkedList } from '../../base/linkedList/linkedList'; import Parent from '../../base/parent'; import IContainerQueryBlock from '../../mixins/containerQueryBlock'; import { ScrollPage } from '../../scrollPage'; diff --git a/packages/core/src/selection/index.ts b/packages/core/src/selection/index.ts index 39251fe..b29969c 100644 --- a/packages/core/src/selection/index.ts +++ b/packages/core/src/selection/index.ts @@ -1,7 +1,4 @@ -import type { - // eslint-disable-next-line import/no-named-default - default as ContentBlock, -} from '../block/base/content'; +import type Content from '../block/base/content'; import type Format from '../block/base/format'; import type Parent from '../block/base/parent'; import type ListItem from '../block/commonMark/listItem'; @@ -134,9 +131,9 @@ class Selection { public doc: Document = document; public anchorPath: (string | number)[] = []; - public anchorBlock: ContentBlock | null = null; + public anchorBlock: Content | null = null; public focusPath: (string | number)[] = []; - public focusBlock: ContentBlock | null = null; + public focusBlock: Content | null = null; public anchor: INodeOffset | null = null; public focus: INodeOffset | null = null; public selectedImage: { @@ -228,8 +225,8 @@ class Selection { if (!anchorDomNode || !focusDomNode) return null; - const anchorBlock = anchorDomNode[BLOCK_DOM_PROPERTY] as ContentBlock; - const focusBlock = focusDomNode[BLOCK_DOM_PROPERTY] as ContentBlock; + const anchorBlock = anchorDomNode[BLOCK_DOM_PROPERTY] as Content; + const focusBlock = focusDomNode[BLOCK_DOM_PROPERTY] as Content; const anchorPath = anchorBlock.path; const focusPath = focusBlock.path;