Skip to content

Commit

Permalink
refactor: use Nullable to replace (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs authored Apr 22, 2024
1 parent b78393c commit 4704795
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 25 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/block/base/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -228,7 +229,7 @@ class Format extends Content {
text: string,
offset: number,
type: Token['type'],
): Token | null {
): Nullable<Token> {
const tokens = tokenizer(text, {
hasBeginRules: false,
options: this.muya.options,
Expand Down Expand Up @@ -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<HTMLElement> = document.querySelector<HTMLElement>(selector);

if (image)
image.click();
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/block/base/linkedList/linkedList.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Nullable } from '../../../types';
import type { ILinkedNode } from './linkedNode';

class LinkedList<T extends ILinkedNode> {
export class LinkedList<T extends ILinkedNode> {
head: Nullable<T> = null;

tail: Nullable<T> = null;
Expand Down Expand Up @@ -118,5 +118,3 @@ class LinkedList<T extends ILinkedNode> {
return [...this.iterator()].reduce(callback, memo);
}
}

export default LinkedList;
2 changes: 1 addition & 1 deletion packages/core/src/block/base/parent.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/block/base/treeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TreeNode> {
if (this.blockName === blockName)
return this;

Expand All @@ -190,7 +190,7 @@ class TreeNode implements ILinkedNode {
return null;
}

farthestBlock(blockName: string): TreeNode | null {
farthestBlock(blockName: string): Nullable<TreeNode> {
const results: TreeNode[] = [];
if (this.blockName === blockName)
results.push(this);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/block/commonMark/bulletList/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/block/commonMark/listItem/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/block/commonMark/orderList/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/block/gfm/table/cell.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/block/gfm/table/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/block/gfm/table/row.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/block/gfm/table/table.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/block/gfm/taskListItem/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
13 changes: 5 additions & 8 deletions packages/core/src/selection/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 4704795

Please sign in to comment.