Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use rxjs to replace eventCenter #166

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 13 additions & 19 deletions packages/core/src/clipboard/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fromEvent, merge } from 'rxjs';
import type Content from '../block/base/content';
import type Parent from '../block/base/parent';
import CodeBlockContent from '../block/content/codeBlockContent';
Expand Down Expand Up @@ -37,22 +38,22 @@ class Clipboard {
constructor(public muya: Muya) {}

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

const copyCutHandler = (event: ClipboardEvent): void => {
const copyCutHandler = (event: Event) => {
event.preventDefault();
event.stopPropagation();

const isCut = event.type === 'cut';

this.copyHandler(event);
this.copyHandler(event as ClipboardEvent);

if (isCut)
this.cutHandler();
};

const keydownHandler = (event: KeyboardEvent): void => {
const { key, metaKey } = event;
const keydownHandler = (event: Event) => {
const { key, metaKey } = event as KeyboardEvent;

const { isSelectionInSameBlock } = this.selection.getSelection() ?? {};
if (isSelectionInSameBlock)
Expand All @@ -75,22 +76,15 @@ class Clipboard {
this.cutHandler();
};

const pasteHandler = (event: ClipboardEvent): void => {
this.pasteHandler(event);
const pasteHandler = (event: Event) => {
this.pasteHandler(event as ClipboardEvent);
};

eventCenter.attachDOMEvent(
domNode,
'copy',
copyCutHandler as EventListener,
);
eventCenter.attachDOMEvent(domNode, 'cut', copyCutHandler as EventListener);
eventCenter.attachDOMEvent(domNode, 'paste', pasteHandler as EventListener);
eventCenter.attachDOMEvent(
domNode,
'keydown',
keydownHandler as EventListener,
);
merge(fromEvent(domNode, 'copy'), fromEvent(domNode, 'cut'))
.subscribe(copyCutHandler);

fromEvent(domNode, 'paste').subscribe(pasteHandler);
fromEvent(domNode, 'keydown').subscribe(keydownHandler);
}

getClipboardData() {
Expand Down
16 changes: 9 additions & 7 deletions packages/core/src/editor/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as otText from 'ot-text-unicode';
import { fromEvent, merge } from 'rxjs';
import { ScrollPage } from '../block/scrollPage';
import type Content from '../block/base/content';
import type Format from '../block/base/format';
Expand Down Expand Up @@ -68,7 +69,6 @@ class Editor {
}

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

const eventHandler = (event: Event) => {
Expand Down Expand Up @@ -121,12 +121,14 @@ class Editor {
}
};

eventCenter.attachDOMEvent(domNode, 'click', eventHandler);
eventCenter.attachDOMEvent(domNode, 'input', eventHandler);
eventCenter.attachDOMEvent(domNode, 'keydown', eventHandler);
eventCenter.attachDOMEvent(domNode, 'keyup', eventHandler);
eventCenter.attachDOMEvent(domNode, 'compositionend', eventHandler);
eventCenter.attachDOMEvent(domNode, 'compositionstart', eventHandler);
merge(
fromEvent(domNode, 'click'),
fromEvent(domNode, 'input'),
fromEvent(domNode, 'keydown'),
fromEvent(domNode, 'keyup'),
fromEvent(domNode, 'compositionend'),
fromEvent(domNode, 'compositionstart'),
).subscribe(eventHandler);
}

focus() {
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.

Loading