Skip to content

Commit

Permalink
refactor: use rxjs to replace eventCenter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs committed Apr 25, 2024
1 parent 4704795 commit 2716fa6
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 29 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"plantuml-encoder": "^1.4.0",
"popper.js": "^1.16.1",
"prismjs": "^1.29.0",
"rxjs": "^7.8.1",
"snabbdom": "^3.6.2",
"snabbdom-to-html": "^7.1.0",
"turndown": "^7.1.3",
Expand Down
22 changes: 11 additions & 11 deletions packages/core/src/block/commonMark/codeBlock/code.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fromEvent } from 'rxjs';
import copyIcon from '../../../assets/icons/copy/2.png';
import Parent from '../../base/parent';
import { ScrollPage } from '../../scrollPage';
Expand Down Expand Up @@ -79,7 +80,11 @@ class Code extends Parent {
}

listen() {
const { eventCenter, editor } = this.muya;
const { editor } = this.muya;

if (this.domNode == null)
return;

// Copy code content to clipboard.
const clickHandler = (event: Event) => {
event.preventDefault();
Expand All @@ -99,16 +104,11 @@ class Code extends Parent {
event.preventDefault();
};

eventCenter.attachDOMEvent(
this.domNode?.firstElementChild as HTMLElement,
'click',
clickHandler,
);
eventCenter.attachDOMEvent(
this.domNode?.firstElementChild as HTMLElement,
'mousedown',
mousedownHandler,
);
const clickObservable = fromEvent(this.domNode.firstElementChild!, 'click');
clickObservable.subscribe(clickHandler);

const mousedownObservable = fromEvent(this.domNode.firstElementChild!, 'mousedown');
mousedownObservable.subscribe(mousedownHandler);
}
}

Expand Down
10 changes: 3 additions & 7 deletions packages/core/src/block/extra/diagram/diagramPreview.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fromEvent } from 'rxjs';
import Parent from '../../base/parent';
import { PREVIEW_DOMPURIFY_CONFIG } from '../../../config';
import type { Muya } from '../../../muya';
Expand Down Expand Up @@ -94,13 +95,8 @@ class DiagramPreview extends Parent {
}

attachDOMEvents() {
const { eventCenter } = this.muya;

eventCenter.attachDOMEvent(
this.domNode!,
'click',
this.clickHandler.bind(this),
);
const clickObservable = fromEvent(this.domNode!, 'click');
clickObservable.subscribe(this.clickHandler.bind(this));
}

clickHandler(event: Event) {
Expand Down
10 changes: 3 additions & 7 deletions packages/core/src/block/extra/math/mathPreview.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import katex from 'katex';
import { fromEvent } from 'rxjs';
import Parent from '../../base/parent';
import type { Muya } from '../../../muya';
import logger from '../../../utils/logger';
Expand Down Expand Up @@ -43,13 +44,8 @@ class MathPreview extends Parent {
}

attachDOMEvents() {
const { eventCenter } = this.muya;

eventCenter.attachDOMEvent(
this.domNode!,
'click',
this.clickHandler.bind(this),
);
const clickObservable = fromEvent(this.domNode!, 'click');
clickObservable.subscribe(this.clickHandler.bind(this));
}

clickHandler(event: Event) {
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/block/gfm/table/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import diff from 'fast-diff';
import { fromEvent } from 'rxjs';
import { LinkedList } from '../../base/linkedList/linkedList';
import Parent from '../../base/parent';
import type TableCellContent from '../../content/tableCell';
Expand Down Expand Up @@ -91,19 +92,20 @@ class Table extends Parent {
}

private _listenDomEvent() {
const { eventCenter } = this.muya;
const { domNode } = this;

// Fix: prevent cursor present at the end of table.
const clickHandler = (event: Event) => {
const mousedownHandler = (event: Event) => {
if (event.target === domNode) {
event.preventDefault();
const cursorBlock = this.lastContentInDescendant()!;
const offset = cursorBlock.text.length;
cursorBlock.setCursor(offset, offset, true);
}
};
eventCenter.attachDOMEvent(domNode!, 'mousedown', clickHandler);

const mousedownObservable = fromEvent(domNode!, 'mousedown');
mousedownObservable.subscribe(mousedownHandler);
}

queryBlock(path: TBlockPath) {
Expand Down
1 change: 1 addition & 0 deletions packages/facade/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Use to export facade APIs.
4 changes: 3 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2716fa6

Please sign in to comment.