Skip to content

Commit

Permalink
refactor: add override
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs committed Dec 24, 2023
1 parent 2c3217a commit 968a9ec
Show file tree
Hide file tree
Showing 45 changed files with 283 additions and 240 deletions.
6 changes: 3 additions & 3 deletions lib/block/base/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Content extends TreeNode {
public _text: string;
public isComposed: boolean;

static blockName = 'content';
static override blockName = 'content';

get hasSelection() {
return !!this.getCursor();
Expand Down Expand Up @@ -197,7 +197,7 @@ class Content extends TreeNode {
}
}

createDomNode() {
override createDomNode() {
super.createDomNode();
this.update();
}
Expand Down Expand Up @@ -537,7 +537,7 @@ class Content extends TreeNode {
return commonAncestors;
}

remove(source = 'user') {
override remove(source = 'user') {
super.remove(source);

return this;
Expand Down
16 changes: 8 additions & 8 deletions lib/block/base/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ const checkTokenIsInlineFormat = (token: Token) => {
};

class Format extends Content {
static blockName = 'format';
static override blockName = 'format';

private _checkCursorInTokenType(
text: string,
Expand Down Expand Up @@ -325,7 +325,7 @@ class Format extends Content {
return false;
}

blurHandler() {
override blurHandler() {
super.blurHandler();
const needRender = this.checkNeedRender();
if (needRender) {
Expand Down Expand Up @@ -450,7 +450,7 @@ class Format extends Content {
eventCenter.emit('muya-image-toolbar', { reference: null });
}

clickHandler(event: Event): void {
override clickHandler(event: Event): void {
if (!isMouseEvent(event)) {
return;
}
Expand Down Expand Up @@ -506,7 +506,7 @@ class Format extends Content {
});
}

keyupHandler(): void {
override keyupHandler(): void {
if (this.isComposed) {
return;
}
Expand Down Expand Up @@ -561,7 +561,7 @@ class Format extends Content {
}
}

inputHandler(event: Event): void {
override inputHandler(event: Event): void {
// Do not use `isInputEvent` util, because compositionEnd event also invoke this method.
if (
this.isComposed ||
Expand Down Expand Up @@ -1237,7 +1237,7 @@ class Format extends Content {
}
}

backspaceHandler(event: Event): void {
override backspaceHandler(event: Event): void {
const { start, end } = this.getCursor() ?? {};
// Let input handler to handle this case.
if (!start || !end || start?.offset !== end?.offset) {
Expand Down Expand Up @@ -1317,7 +1317,7 @@ class Format extends Content {
}
}

deleteHandler(event: KeyboardEvent): void {
override deleteHandler(event: KeyboardEvent): void {
const { start, end } = this.getCursor()!;
const { text } = this;
// Let input handler to handle this case.
Expand Down Expand Up @@ -1358,7 +1358,7 @@ class Format extends Content {
this.setCursor(start.offset + 1, end.offset + 1, true);
}

enterHandler(event: KeyboardEvent): void {
override enterHandler(event: KeyboardEvent): void {
event.preventDefault();
const { text: oldText, muya, parent } = this;
const { start, end } = this.getCursor()!;
Expand Down
27 changes: 17 additions & 10 deletions lib/block/base/linkedList/linkedList.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import LinkedNode from './linkedNode';
import { Nullable } from '@muya/types';
import type { LinkedNode } from './linkedNode';

class LinkedList<T extends LinkedNode<T>> {
public head: T | null = null;
public tail: T | null = null;
public length: number = 0;
class LinkedList<T extends LinkedNode> {
head: Nullable<T> = null;

*iterator(curNode: T | null = this.head, length = this.length) {
tail: Nullable<T> = null;

length: number = 0;

*iterator(curNode = this.head, length = this.length) {
let count = 0;

while (count < length && curNode) {
yield curNode;
count++;
curNode = curNode.next;
curNode = curNode.next as T;
}
}

Expand Down Expand Up @@ -74,11 +77,11 @@ class LinkedList<T extends LinkedNode<T>> {
}

if (this.head === node) {
this.head = node.next;
this.head = node.next as T;
}

if (this.tail === node) {
this.tail = node.prev;
this.tail = node.prev as T;
}
this.length -= 1;
}
Expand All @@ -95,7 +98,11 @@ class LinkedList<T extends LinkedNode<T>> {
return [...this.iterator()].forEach(callback);
}

forEachAt(index: number, length: number = this.length, callback: (cur: T, i: number) => void) {
forEachAt(
index: number,
length: number = this.length,
callback: (cur: T, i: number) => void
) {
const curNode = this.find(index);

return [...this.iterator(curNode, length)].forEach((node, i) => {
Expand Down
9 changes: 4 additions & 5 deletions lib/block/base/linkedList/linkedNode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Nullable } from '@muya/types';

class LinkedNode<T> {
public prev: Nullable<T> = null;
public next: Nullable<T> = null;
}
export interface LinkedNode {
prev: Nullable<LinkedNode>;

export default LinkedNode;
next: Nullable<LinkedNode>;
}
13 changes: 8 additions & 5 deletions lib/block/base/parent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ const debug = logger('parent:');

class Parent extends TreeNode {
// Used to store icon, checkbox(span) etc. these blocks are not in children properties in json state.
public attachments: LinkedList<Parent> = new LinkedList();
public children: LinkedList<TreeNode> = new LinkedList();
public prev: Nullable<Parent> = null;
public next: Nullable<Parent> = null;
attachments: LinkedList<Parent> = new LinkedList();

children: LinkedList<TreeNode> = new LinkedList();

override prev: Nullable<Parent> = null;

override next: Nullable<Parent> = null;

private _active: boolean = false;

Expand Down Expand Up @@ -209,7 +212,7 @@ class Parent extends TreeNode {
return newNode;
}

remove(source = 'user') {
override remove(source = 'user') {
if (source === 'user') {
// dispatch json1 operation
const path = this.getJsonPath();
Expand Down
54 changes: 28 additions & 26 deletions lib/block/base/treeNode.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import LinkedNode from '@muya/block/base/linkedList/linkedNode';
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> {
Expand All @@ -14,18 +14,22 @@ interface IConstructor<T> {
new (muya: Muya): T;
}

class TreeNode extends LinkedNode<TreeNode> {
public parent: Nullable<Parent> = null;
class TreeNode implements LinkedNode {
prev: Nullable<TreeNode> = null;

public domNode: Nullable<HTMLElement> = null;
next: Nullable<TreeNode> = null;

public tagName: string = '';
parent: Nullable<Parent> = null;

public classList: string[] = [];
domNode: Nullable<HTMLElement> = null;

public attributes: Attributes = {};
tagName: string = '';

public datasets: Datasets = {};
classList: string[] = [];

attributes: Attributes = {};

datasets: Datasets = {};

static blockName = 'tree.node';

Expand Down Expand Up @@ -53,8 +57,8 @@ class TreeNode extends LinkedNode<TreeNode> {
return this.parent ? this.parent.isScrollPage : false;
}

get outMostBlock(): this | Parent | null {
let node = this.isContent() ? this.parent : this;
get outMostBlock(): Nullable<Parent> {
let node = this.isContent() ? this.parent : this as unknown as Parent;

while (node) {
if (node.isOutMostBlock) {
Expand All @@ -66,27 +70,25 @@ class TreeNode extends LinkedNode<TreeNode> {
return null;
}

constructor(public muya: Muya) {
super();
}
constructor(public muya: Muya) {}

/**
/**
* check this is a Content block?
* @param this
* @returns boolean
*/
isContent(this: TreeNode): this is Content {
return 'text' in this;
}
/**
* check this is a Parent block?
* @param this
* @returns boolean
*/
isParent(this: unknown): this is Parent {
return this instanceof Parent;
}
isContent(this: TreeNode): this is Content {
return 'text' in this;
}

/**
* check this is a Parent block?
* @param this
* @returns boolean
*/
isParent(this: unknown): this is Parent {
return this instanceof Parent;
}

/**
* create domNode
Expand Down
6 changes: 3 additions & 3 deletions lib/block/commonMark/atxHeading/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { IAtxHeadingState } from '../../../state/types';
class AtxHeading extends Parent {
public meta: IAtxHeadingState['meta'];

static blockName = 'atx-heading';
static override blockName = 'atx-heading';

static create(muya: Muya, state: IAtxHeadingState) {
const heading = new AtxHeading(muya, state);
Expand All @@ -23,7 +23,7 @@ class AtxHeading extends Parent {
return heading;
}

get path(): TBlockPath {
override get path(): TBlockPath {
const { path: pPath } = this.parent!;
const offset = this.parent!.offset(this);

Expand All @@ -38,7 +38,7 @@ class AtxHeading extends Parent {
this.createDomNode();
}

getState(): IAtxHeadingState {
override getState(): IAtxHeadingState {
return {
name: 'atx-heading',
meta: this.meta,
Expand Down
6 changes: 3 additions & 3 deletions lib/block/commonMark/blockQuote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IBlockQuoteState } from '../../../state/types';

@mixins(ContainerQueryBlock)
class BlockQuote extends Parent {
static blockName = 'block-quote';
static override blockName = 'block-quote';

static create(muya: Muya, state: IBlockQuoteState) {
const blockQuote = new BlockQuote(muya);
Expand All @@ -19,7 +19,7 @@ class BlockQuote extends Parent {
return blockQuote;
}

get path() {
override get path() {
const { path: pPath } = this.parent!;
const offset = this.parent!.offset(this);

Expand All @@ -33,7 +33,7 @@ class BlockQuote extends Parent {
this.createDomNode();
}

getState(): IBlockQuoteState {
override getState(): IBlockQuoteState {
const state: IBlockQuoteState = {
name: 'block-quote',
children: this.children.map((child) => (child as Parent).getState()),
Expand Down
8 changes: 4 additions & 4 deletions lib/block/commonMark/bulletList/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { mixins } from '@muya/utils';

@mixins(ContainerQueryBlock)
class BulletList extends Parent {
public children: LinkedList<Parent> = new LinkedList();
public override children: LinkedList<Parent> = new LinkedList();

static blockName = 'bullet-list';
static override blockName = 'bullet-list';

static create(muya: Muya, state: IBulletListState) {
const bulletList = new BulletList(muya, state);
Expand All @@ -24,7 +24,7 @@ class BulletList extends Parent {
return bulletList;
}

get path() {
override get path() {
const { path: pPath } = this.parent!;
const offset = this.parent!.offset(this);

Expand All @@ -47,7 +47,7 @@ class BulletList extends Parent {
this.createDomNode();
}

getState(): IBulletListState {
override getState(): IBulletListState {
const state: IBulletListState = {
name: 'bullet-list',
meta: { ...this.meta },
Expand Down
Loading

0 comments on commit 968a9ec

Please sign in to comment.