Skip to content

Commit

Permalink
fix: typescript check error
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs committed Jan 14, 2024
1 parent 21996df commit 7c9834e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 48 deletions.
8 changes: 1 addition & 7 deletions packages/block/base/treeNode.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { BLOCK_DOM_PROPERTY } from '@muya/config';
import Muya from '@muya/index';
import type { TState } from '@muya/state/types';
import { Nullable } from '@muya/types';
import { createDomNode } from '@muya/utils/dom';
import type { Attributes, Datasets } from '@muya/utils/types';
import Content from './content';
import type { LinkedNode } from './linkedList/linkedNode';
import Parent from './parent';

interface IConstructor<T> {
blockName: string;
create: (muya: Muya, state: TState) => T;
new (muya: Muya): T;
}
import { IConstructor } from '../types';

class TreeNode implements LinkedNode {
prev: Nullable<TreeNode> = null;
Expand Down
96 changes: 57 additions & 39 deletions packages/block/content/paragraphContent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import type {
ITaskListItemState,
} from '../../../state/types';

enum UnindentType {
INDENT = 'INDENT',
REPLACEMENT = 'REPLACEMENT',
}

const debug = logger('paragraph:content');

const HTML_BLOCK_REG = /^<([a-zA-Z\d-]+)(?=\s|>)[^<>]*?>$/;
Expand Down Expand Up @@ -59,6 +64,7 @@ const parseTableHeader = (text: string) => {

return rowHeader;
};

/**
* ParagraphContent
*/
Expand Down Expand Up @@ -109,15 +115,14 @@ class ParagraphContent extends Format {

switch (type) {
case 'paragraph':
return this.handleBackspaceInParagraph();
return this._handleBackspaceInParagraph();

case 'block-quote':
return this.handleBackspaceInBlockQuote();
return this._handleBackspaceInBlockQuote();

case 'list-item':
// fall through
case 'list-item': // fall through
case 'task-list-item':
return this.handleBackspaceInList();
return this._handleBackspaceInList();

default:
debug.error('Unknown backspace type');
Expand All @@ -132,9 +137,10 @@ class ParagraphContent extends Format {
eventCenter.emit('content-change', { block: this });
}

enterConvert(event: Event) {
private _enterConvert(event: Event) {
event.preventDefault();
event.stopPropagation();

const TABLE_BLOCK_REG = /^\|.*?(\\*)\|.*?(\\*)\|/;
const MATH_BLOCK_REG = /^\$\$/;
const { text } = this;
Expand Down Expand Up @@ -213,7 +219,7 @@ class ParagraphContent extends Format {
}
}

enterInBlockQuote(event: Event) {
private _enterInBlockQuote(event: Event) {
const { text, parent } = this;
if (text.length !== 0) {
return super.enterHandler(event as KeyboardEvent);
Expand Down Expand Up @@ -266,7 +272,7 @@ class ParagraphContent extends Format {
(newNode.children.head as ParagraphContent).setCursor(0, 0, true);
}

enterInListItem(event: Event) {
private _enterInListItem(event: Event) {
event.preventDefault();
event.stopPropagation();

Expand Down Expand Up @@ -399,11 +405,11 @@ class ParagraphContent extends Format {
const type = this._paragraphParentType();

if (type === 'block-quote') {
this.enterInBlockQuote(event);
this._enterInBlockQuote(event);
} else if (type === 'list-item' || type === 'task-list-item') {
this.enterInListItem(event);
this._enterInListItem(event);
} else {
this.enterConvert(event);
this._enterConvert(event);
}
}

Expand Down Expand Up @@ -433,7 +439,7 @@ class ParagraphContent extends Format {
return type;
}

handleBackspaceInParagraph(this: ParagraphContent) {
private _handleBackspaceInParagraph(this: ParagraphContent) {
const previousContentBlock = this.previousContentInContext();
// Handle no previous content block, the first paragraph in document.
if (!previousContentBlock) {
Expand All @@ -446,13 +452,13 @@ class ParagraphContent extends Format {
previousContentBlock.setCursor(offset, offset, true);
}

handleBackspaceInBlockQuote() {
private _handleBackspaceInBlockQuote() {
const parent = this.parent!;
const blockQuote = parent!.parent!;
let cursorBlock: Content | null;

if (!parent!.isOnlyChild() && !parent!.isFirstChild()) {
return this.handleBackspaceInParagraph();
return this._handleBackspaceInParagraph();
}

if (parent.isOnlyChild()) {
Expand All @@ -468,13 +474,13 @@ class ParagraphContent extends Format {
cursorBlock!.setCursor(0, 0, true);
}

handleBackspaceInList() {
private _handleBackspaceInList() {
const parent = this.parent!;
const listItem = parent.parent!;
const list = listItem.parent!;

if (!parent.isFirstChild()) {
return this.handleBackspaceInParagraph();
return this._handleBackspaceInParagraph();
}

if (listItem.isOnlyChild()) {
Expand Down Expand Up @@ -511,25 +517,25 @@ class ParagraphContent extends Format {
}
}

isUnindentableListItem() {
private _getUnindentType(): Nullable<UnindentType> {
if (!this.isCollapsed) {
return null;
}

const { parent } = this;
const listItem = parent!.parent;
const list = listItem?.parent;
const listParent = list?.parent;

if (!this.isCollapsed) {
return false;
}

if (
listParent &&
(listParent.blockName === 'list-item' ||
listParent.blockName === 'task-list-item')
) {
return list.prev ? 'INDENT' : 'REPLACEMENT';
return list.prev ? UnindentType.INDENT : UnindentType.REPLACEMENT;
}

return false;
return null;
}

private _canIndentListItem() {
Expand Down Expand Up @@ -557,7 +563,7 @@ class ParagraphContent extends Format {
return list && /ol|ul/.test(list.tagName) && listItem.prev;
}

private _unindentListItem(type: string) {
private _unindentListItem(type: UnindentType) {
const { parent } = this;
const listItem = parent?.parent;
const list = listItem?.parent;
Expand All @@ -578,7 +584,7 @@ class ParagraphContent extends Format {

const cursorParagraphOffset = listItem.offset(parent);

if (type === 'REPLACEMENT') {
if (type === UnindentType.REPLACEMENT) {
const paragraph = parent.clone() as Paragraph;
listParent.insertBefore(paragraph, list);

Expand All @@ -587,7 +593,7 @@ class ParagraphContent extends Format {
} else {
listItem.remove();
}
} else if (type === 'INDENT') {
} else if (type === UnindentType.INDENT) {
const newListItem = listItem.clone() as Parent;
listParent.parent!.insertAfter(newListItem, listParent);

Expand All @@ -612,15 +618,23 @@ class ParagraphContent extends Format {
const offset = list.offset(listItem);

list.forEachAt(offset + 1, undefined, (node) => {
newListItem.lastChild?.append((node as Parent).clone(), 'user');
(newListItem.lastChild as Parent).append(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(node as any).clone(),
'user'
);
node.remove();
});
}

if (list.next) {
const offset = listParent.offset(list);
listParent.forEachAt(offset + 1, undefined, (node) => {
newListItem.lastChild?.append((node as Parent).clone(), 'user');
(newListItem.lastChild as Parent).append(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(node as any).clone(),
'user'
);
node.remove();
});
}
Expand All @@ -636,13 +650,15 @@ class ParagraphContent extends Format {
return;
}

const cursorBlock = (newListItem.find(cursorParagraphOffset) as Parent).firstContentInDescendant();
const cursorBlock = (
newListItem.find(cursorParagraphOffset) as Parent
).firstContentInDescendant();

cursorBlock?.setCursor(start.offset, end.offset, true);
}
}

indentListItem() {
private _indentListItem() {
const { parent, muya } = this;
const listItem = parent?.parent;
const list = listItem?.parent;
Expand All @@ -662,18 +678,20 @@ class ParagraphContent extends Format {
if (!newList || !/ol|ul/.test(newList.tagName)) {
const state = {
name: list.blockName,
meta: { ...list.meta },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
meta: { ...(list as any).meta },
children: [listItem.getState()],
};
newList = ScrollPage.loadBlock(state.name).create(muya, state);
prevListItem!.append(newList, 'user');
prevListItem!.append(newList as Parent, 'user');
} else {
newList.append(listItem.clone(), 'user');
(newList as Parent).append(listItem.clone() as Parent, 'user');
}

listItem.remove();

const cursorBlock = newList!.lastChild
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cursorBlock = ((newList as Parent).lastChild as any)
.find(offset)
.firstContentInDescendant();
cursorBlock.setCursor(start.offset, end.offset, true);
Expand Down Expand Up @@ -837,13 +855,13 @@ class ParagraphContent extends Format {
}

if (event.shiftKey) {
const unindentType = this.isUnindentableListItem();
const unindentType = this._getUnindentType();

if (unindentType) {
this._unindentListItem(unindentType);
if (unindentType == null) {
return;
}

return;
this._unindentListItem(unindentType);
}

// Handle `tab` to jump to the end of format when the cursor is at the end of format content.
Expand All @@ -859,7 +877,7 @@ class ParagraphContent extends Format {
}

if (this._canIndentListItem()) {
this.indentListItem();
this._indentListItem();
return;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@ ScrollPage.register(DiagramBlock);
ScrollPage.register(DiagramContainer);
ScrollPage.register(DiagramPreview);

export default ScrollPage;
const ScrollPageForExport = ScrollPage;

export default ScrollPageForExport;
3 changes: 2 additions & 1 deletion packages/block/scrollPage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class ScrollPage extends Parent {

static registeredBlocks = new Map();

static register(Block: Parent | Content) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static register(Block: any) {
const { blockName } = Block;
this.registeredBlocks.set(blockName, Block);
}
Expand Down
9 changes: 9 additions & 0 deletions packages/block/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
import Muya from '@muya/index';

export interface IConstructor<T> {
blockName: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
create: (muya: Muya, state: any) => T;
new (muya: Muya): T;
}

export type TBlockPath = (string | number)[];

0 comments on commit 7c9834e

Please sign in to comment.