Skip to content

Commit

Permalink
refactor: parent block
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs committed Dec 17, 2023
1 parent 323717c commit 08d2643
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 57 deletions.
4 changes: 2 additions & 2 deletions lib/block/base/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
isKeyboardEvent,
} from '@muya/utils';
import diff from 'fast-diff';
import { TPathList } from '../types';
import { TBlockPath } from '../types';

// import logger from '@muya/utils/logger'

Expand All @@ -36,7 +36,7 @@ class Content extends TreeNode {
return this.muya.editor.inlineRenderer;
}

get path(): TPathList {
get path(): TBlockPath {
if (this.parent == null) {
return ['text'];
}
Expand Down
71 changes: 38 additions & 33 deletions lib/block/base/parent.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import LinkedList from '@muya/block/base/linkedList/linkedList';
import TreeNode from '@muya/block/base/treeNode';
import { CLASS_NAMES } from '@muya/config';
import Muya from '@muya/index';
import { TState } from '@muya/state/types';
import { Nullable } from '@muya/types';
import { operateClassName } from '@muya/utils/dom';
import logger from '@muya/utils/logger';
import { TPathList } from '../types';
import Content from './content';
import { TBlockPath } from '../types';

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: Parent | null = null;
public next: Parent | null = null;
public prev: Nullable<Parent> = null;
public next: Nullable<Parent> = null;

private _active: boolean = false;

Expand All @@ -25,10 +24,17 @@ class Parent extends TreeNode {

set active(value) {
this._active = value;

if (this.domNode == null) {
debug.error('domNode is null.');

return;
}

if (value) {
operateClassName(this.domNode!, 'add', CLASS_NAMES.MU_ACTIVE);
operateClassName(this.domNode, 'add', CLASS_NAMES.MU_ACTIVE);
} else {
operateClassName(this.domNode!, 'remove', CLASS_NAMES.MU_ACTIVE);
operateClassName(this.domNode, 'remove', CLASS_NAMES.MU_ACTIVE);
}
}

Expand All @@ -46,15 +52,12 @@ class Parent extends TreeNode {
);
}

get path(): TPathList {
get path(): TBlockPath {
// You should never call get path on Parent.
debug.error('You should never call get path on Parent.');
return [];
}

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

getJsonPath() {
const { path } = this;
if (this.isContainerBlock) {
Expand All @@ -65,7 +68,8 @@ class Parent extends TreeNode {
}

getState(): TState {
// You should never call get path on Parent.
// You should never call get state on Parent.
debug.error('You should never call get state on Parent.')
return {} as TState;
}

Expand Down Expand Up @@ -242,51 +246,52 @@ class Parent extends TreeNode {
/**
* find the first content block, paragraph.content etc.
*/
firstContentInDescendant(): Content {
let firstContentBlock: Content | any = this;
do {
firstContentBlock = firstContentBlock.children.head;
} while (firstContentBlock.children);
firstContentInDescendant() {
let likeContentBlock: Nullable<TreeNode> = this.children.head;

return firstContentBlock;
while (likeContentBlock && likeContentBlock.isParent()) {
likeContentBlock = likeContentBlock.children.head;
}

return likeContentBlock?.isContent() ? likeContentBlock : null;
}

/**
* find the last content block in container block.
*/
lastContentInDescendant(): Content {
let lastContentBlock: Content | any = this;
lastContentInDescendant() {
let likeContentBlock: Nullable<TreeNode> = this.children.tail;

do {
lastContentBlock = lastContentBlock.children.tail;
} while (lastContentBlock.children);
while(likeContentBlock && likeContentBlock.isParent()) {
likeContentBlock = likeContentBlock.children.tail;
}

return lastContentBlock;
return likeContentBlock?.isContent() ? likeContentBlock : null;
}

breadthFirstTraverse(callback) {
const queue = [this];
breadthFirstTraverse(this: Parent, callback: (node: TreeNode) => void) {
const queue: TreeNode[] = [this];

while (queue.length) {
const node = queue.shift();
const node = queue.shift()!;

callback(node);

if (node.children) {
if (node.isParent()) {
node.children.forEach((child) => queue.push(child));
}
}
}

depthFirstTraverse(callback: (node: Parent | Content) => void) {
const stack: Parent | Content = [this];
depthFirstTraverse(this: Parent, callback: (node: TreeNode) => void) {
const stack: TreeNode[] = [this];

while (stack.length) {
const node = stack.shift();
const node = stack.shift()!;

callback(node);

if (node.children) {
if (node.isParent()) {
// Use splice ot make sure the first block in document is process first.
node.children.forEach((child, i) => stack.splice(i, 0, child));
}
Expand Down
4 changes: 2 additions & 2 deletions lib/block/commonMark/atxHeading/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Content from '@muya/block/base/content';
import Parent from '@muya/block/base/parent';
import LeafQueryBlock from '@muya/block/mixins/leafQueryBlock';
import ScrollPage from '@muya/block/scrollPage';
import { TPathList } from '@muya/block/types';
import { TBlockPath } from '@muya/block/types';
import Muya from '@muya/index';
import { mixins } from '@muya/utils';
import { IAtxHeadingState } from '../../../state/types';
Expand All @@ -27,7 +27,7 @@ class AtxHeading extends Parent {
return heading;
}

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

Expand Down
6 changes: 3 additions & 3 deletions lib/block/commonMark/codeBlock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import logger from '@muya/utils/logger';
import { loadLanguage } from '@muya/utils/prism';
import diff from 'fast-diff';
import { ICodeBlockState } from '../../../state/types';
import { TPathList } from '../../types';
import { TBlockPath } from '../../types';

const debug = logger('codeblock:');

Expand Down Expand Up @@ -81,7 +81,7 @@ class CodeBlock extends Parent {
});
}

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

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

queryBlock(path: TPathList) {
queryBlock(path: TBlockPath) {
if (path.length === 0) {
return this;
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/block/commonMark/html/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Parent from '@muya/block/base/parent';
import ScrollPage from '@muya/block/scrollPage';
import { TPathList } from '@muya/block/types';
import { TBlockPath } from '@muya/block/types';
import { CLASS_NAMES } from '@muya/config';
import Muya from '@muya/index';
import { IHtmlBlockState } from '../../../state/types';
Expand Down Expand Up @@ -44,7 +44,7 @@ class HTMLBlock extends Parent {
this.createDomNode();
}

queryBlock(path: TPathList) {
queryBlock(path: TBlockPath) {
return path.length && path[0] === 'text'
? this.firstContentInDescendant()
: this;
Expand Down
4 changes: 2 additions & 2 deletions lib/block/extra/diagram/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Parent from '@muya/block/base/parent';
import ScrollPage from '@muya/block/scrollPage';
import { TPathList } from '@muya/block/types';
import { TBlockPath } from '@muya/block/types';
import Muya from '@muya/index';
import logger from '@muya/utils/logger';
import { loadLanguage } from '@muya/utils/prism';
Expand Down Expand Up @@ -63,7 +63,7 @@ class DiagramBlock extends Parent {
this.createDomNode();
}

queryBlock(path: TPathList) {
queryBlock(path: TBlockPath) {
return path.length && path[0] === 'text'
? this.firstContentInDescendant()
: this;
Expand Down
4 changes: 2 additions & 2 deletions lib/block/extra/frontmatter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ScrollPage from '@muya/block/scrollPage';
// import { diffToTextOp } from '@muya/utils'
import { loadLanguage } from '@muya/utils/prism';
// import { operateClassName } from '@muya/utils/dom'
import { TPathList } from '@muya/block/types';
import { TBlockPath } from '@muya/block/types';
import Muya from '@muya/index';
import logger from '@muya/utils/logger';
import { IFrontmatterMeta, IFrontmatterState } from '../../../state/types';
Expand Down Expand Up @@ -71,7 +71,7 @@ class Frontmatter extends Parent {
this.createDomNode();
}

queryBlock(path: TPathList) {
queryBlock(path: TBlockPath) {
if (path.length === 0) {
return this;
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/block/extra/math/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Parent from '@muya/block/base/parent';
import ScrollPage from '@muya/block/scrollPage';
import { TPathList } from '@muya/block/types';
import { TBlockPath } from '@muya/block/types';
import Muya from '@muya/index';
import { IMathBlockState, IMathMeta } from '../../../state/types';

Expand Down Expand Up @@ -42,7 +42,7 @@ class MathBlock extends Parent {
this.createDomNode();
}

queryBlock(path: TPathList) {
queryBlock(path: TBlockPath) {
return path.length && path[0] === 'text'
? this.firstContentInDescendant()
: this;
Expand Down
4 changes: 2 additions & 2 deletions lib/block/gfm/table/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Parent from '@muya/block/base/parent';
import ScrollPage from '@muya/block/scrollPage';
import { TPathList } from '@muya/block/types';
import { TBlockPath } from '@muya/block/types';
import Muya from '@muya/index';
import { diffToTextOp } from '@muya/utils';
import logger from '@muya/utils/logger';
Expand Down Expand Up @@ -100,7 +100,7 @@ class Table extends Parent {
eventCenter.attachDOMEvent(domNode!, 'mousedown', clickHandler);
}

queryBlock(path: TPathList) {
queryBlock(path: TBlockPath) {
return (this.firstChild as TableInner).queryBlock(path);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/block/gfm/taskListItem/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Parent from '@muya/block/base/parent';
import ContainerQueryBlock from '@muya/block/mixins/containerQueryBlock';
import ScrollPage from '@muya/block/scrollPage';
import { TPathList } from '@muya/block/types';
import { TBlockPath } from '@muya/block/types';
import Muya from '@muya/index';
import { mixins } from '@muya/utils';
import { ITaskListItemMeta, ITaskListItemState } from '../../../state/types';
Expand All @@ -27,7 +27,7 @@ class TaskListItem extends Parent {
return listItem;
}

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

Expand Down
7 changes: 3 additions & 4 deletions lib/block/scrollPage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { isMouseEvent } from '@muya/utils';
import logger from '@muya/utils/logger';
import { TState } from '../../state/types';
import Content from '../base/content';
import TreeNode from '../base/treeNode';
import { TPathList } from '../types';
import { TBlockPath } from '../types';

const debug = logger('scrollpage:');

Expand Down Expand Up @@ -95,7 +94,7 @@ class ScrollPage extends Parent {
* Find the content block by the path
* @param {array} path
*/
queryBlock(path: TPathList) {
queryBlock(path: TBlockPath) {
if (path.length === 0) {
return this;
}
Expand All @@ -109,7 +108,7 @@ class ScrollPage extends Parent {
updateRefLinkAndImage(label: string) {
const REG = new RegExp(`\\[${label}\\](?!:)`);

this.breadthFirstTraverse((node: TreeNode) => {
this.breadthFirstTraverse((node) => {
if (node.isContent() && REG.test(node.text)) {
node.update();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/block/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type TPathList = (string | number)[];
export type TBlockPath = (string | number)[];

0 comments on commit 08d2643

Please sign in to comment.