-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0de9eb6
commit afa36a8
Showing
6 changed files
with
98 additions
and
264 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>wangEditor example</title> | ||
<style> | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<p> | ||
wangEditor demo | ||
</p> | ||
<div id="div1"> | ||
<p>欢迎使用 <b>wangEditor</b> 富文本编辑器</p> | ||
</div> | ||
|
||
<div id="div2"> | ||
<p>欢迎使用 <b>wangEditor</b> 富文本编辑器</p> | ||
</div> | ||
|
||
<script src="../dist/wangEditor.js"></script> | ||
<script> | ||
// 改为使用var声明,才能在window对象上获取到编辑器实例,方便e2e测试 | ||
var E = window.wangEditor | ||
var editor = new E('#div1') | ||
editor.config.onSelectionChange = function (newHtml) { | ||
console.log('editor', newHtml) | ||
} | ||
editor.create() | ||
|
||
// setTimeout(() => { | ||
// console.log("摧毁了") | ||
// editor.destroy() | ||
// }, 3000) | ||
|
||
// var editor2 = new E('#div2') | ||
// editor2.config.onSelectionChange = function (newHtml) { | ||
// console.log('editor2', newHtml) | ||
// } | ||
// editor2.create() | ||
|
||
// setTimeout(() => { | ||
// console.log("摧毁了") | ||
// editor2.destroy() | ||
// }, 3000) | ||
</script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* @description range变化 | ||
* @author liuwei | ||
*/ | ||
import Editor from '../index' | ||
export default class SelectionChange { | ||
constructor(public editor: Editor) { | ||
// 绑定的事件 | ||
const init = () => { | ||
const activeElement = document.activeElement | ||
if (activeElement === editor.$textElem.elems[0]) { | ||
this.emit() | ||
} | ||
} | ||
|
||
// 选取变化事件监听 | ||
window.document.addEventListener('selectionchange', init) | ||
|
||
// 摧毁时移除监听 | ||
this.editor.beforeDestroy(() => { | ||
window.document.removeEventListener('selectionchange', init) | ||
}) | ||
} | ||
|
||
public emit(): void { | ||
// 执行rangeChange函数 | ||
const { onSelectionChange } = this.editor.config | ||
if (onSelectionChange) { | ||
const selection = this.editor.selection | ||
selection.saveRange() | ||
if (!selection.isSelectionEmpty()) | ||
onSelectionChange({ | ||
// 当前文本 | ||
text: selection.getSelectionText(), | ||
// 当前的html | ||
html: selection.getSelectionContainerElem()?.elems[0].innerHTML, | ||
// select对象 | ||
selection: selection, | ||
}) | ||
} | ||
} | ||
} |