Skip to content

Commit

Permalink
fix: destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
吴君 committed Oct 31, 2024
1 parent 5f3cbbb commit 4a3874f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 44 deletions.
53 changes: 27 additions & 26 deletions examples/index.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/logo.jpg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Muya Example</title>
</head>
<body>
<div class="tools">
<button id="undo">UNDO</button>
<button id="redo">REDO</button>
<input id="search" type="text" placeholder="Search text...">
<button id="previous">Previous</button>
<button id="next">Next</button>
<input id="replace" type="text" placeholder="Replace text...">
<button id="single">Single</button>
<button id="all">All</button>
<button id="set-content">Set Content</button>
<button id="select-all">Select All</button>
</div>
<div class="editor-container">
<div id="editor"></div>
</div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/logo.jpg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Muya Example</title>
</head>
<body>
<div class="tools">
<button id="undo">UNDO</button>
<button id="redo">REDO</button>
<input id="search" type="text" placeholder="Search text..." />
<button id="previous">Previous</button>
<button id="next">Next</button>
<input id="replace" type="text" placeholder="Replace text..." />
<button id="single">Single</button>
<button id="all">All</button>
<button id="set-content">Set Content</button>
<button id="select-all">Select All</button>
<button id="destroy">Destroy</button>
</div>
<div class="editor-container">
<div id="editor"></div>
</div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
28 changes: 14 additions & 14 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "muya-examples",
"version": "0.0.1",
"description": "Muya vanilla ts demo project",
"author": "jocs <[email protected]>",
"scripts": {
"dev:demo": "vite"
},
"keywords": [],
"license": "MIT",
"dependencies": {
"@muyajs/core": "workspace:*",
"intl-segmenter-polyfill": "^0.4.4"
}
}
"name": "muya-examples",
"version": "0.0.1",
"description": "Muya vanilla ts demo project",
"author": "jocs <[email protected]>",
"license": "MIT",
"keywords": [],
"scripts": {
"dev:demo": "vite"
},
"dependencies": {
"@muyajs/core": "workspace:*",
"intl-segmenter-polyfill": "^0.4.4"
}
}
5 changes: 5 additions & 0 deletions examples/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const allBtn: HTMLButtonElement = document.querySelector('#all')!;
const setContentBtn: HTMLButtonElement
= document.querySelector('#set-content')!;
const selectAllBtn: HTMLButtonElement = document.querySelector('#select-all')!;
const destroyBtn: HTMLButtonElement = document.querySelector('#destroy')!;

const muya = new Muya(container, {
markdown: DEFAULT_MARKDOWN,
Expand Down Expand Up @@ -115,6 +116,10 @@ selectAllBtn.addEventListener('click', () => {
muya.selectAll();
});

destroyBtn.addEventListener('click', () => {
muya.destroy();
});

const content = [
{
name: 'paragraph',
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/muya.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,14 @@ export class Muya {
this.domNode.remove();

// Hide all float tools.
if (this.ui)
if (this.ui) {
this.ui.hideAllFloatTools();
this.ui.destroy();
}

for (const plugin of Object.values(this._uiPlugins)) {
plugin?.destroy?.();
}
}
}

Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/ui/imageResizeBar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export class ImageResizeBar {
if (
!this.resizing
&& this.status
&& Math.abs((event.target as HTMLElement).scrollTop - this.lastScrollTop) > 50
&& Math.abs((event.target as HTMLElement).scrollTop - this.lastScrollTop)
> 50
) {
this.hide();
}
Expand Down Expand Up @@ -181,8 +182,7 @@ export class ImageResizeBar {
event.preventDefault();
const { eventCenter } = this.muya;
if (this.eventId.length) {
for (const id of this.eventId)
eventCenter.detachDOMEvent(id);
for (const id of this.eventId) eventCenter.detachDOMEvent(id);

this.eventId = [];
}
Expand All @@ -204,4 +204,9 @@ export class ImageResizeBar {
this.status = false;
eventCenter.emit('muya-float', this, false);
}

destroy() {
if (this.container)
this.container?.remove();
}
}
8 changes: 8 additions & 0 deletions packages/core/src/ui/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ export class Ui {
for (const btn of this.shownButton)
btn.hide();
}

destroy() {
for (const tool of this.shownFloat)
tool.destroy();

for (const btn of this.shownButton)
btn.destroy();
}
}

0 comments on commit 4a3874f

Please sign in to comment.