81 lines
2.5 KiB
HTML
81 lines
2.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>协同编辑器</title>
|
|
<link href="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css" rel="stylesheet">
|
|
<style>
|
|
#editor—wrapper {
|
|
border: 1px solid #ccc;
|
|
z-index: 100; /* 按需定义 */
|
|
}
|
|
#toolbar-container { border-bottom: 1px solid #ccc; }
|
|
#editor-container { height: 500px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="editor—wrapper">
|
|
<div id="toolbar-container"><!-- 工具栏 --></div>
|
|
<div id="editor-container"><!-- 编辑器 --></div>
|
|
</div>
|
|
<textarea id="editor" placeholder="Start editing..."></textarea>
|
|
|
|
<script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
|
|
<script>
|
|
const { createEditor, createToolbar } = window.wangEditor
|
|
document.addEventListener('DOMContentLoaded', (event) => {
|
|
const editor_content = editor.getHtml()
|
|
const ws = new WebSocket('ws://localhost:6789');
|
|
|
|
ws.onopen = function(event) {
|
|
console.log('Connection established!');
|
|
};
|
|
|
|
ws.onmessage = function(event) {
|
|
// 当接收到消息时,更新编辑器的内容
|
|
const message = JSON.parse(event.data);
|
|
editor_content.value = message; // 假设消息直接是编辑内容
|
|
console.log(editor_content.value)
|
|
};
|
|
|
|
ws.onclose = function(event) {
|
|
console.log('Connection closed!');
|
|
};
|
|
|
|
editor.oninput = function(event) {
|
|
// 当编辑器内容发生变化时,发送消息
|
|
const message = {
|
|
type: 'edit',
|
|
content: editor_content.value
|
|
};
|
|
ws.send(JSON.stringify(message));
|
|
};
|
|
});
|
|
const editorConfig = {
|
|
placeholder: 'Type here...',
|
|
onChange(editor) {
|
|
const html = editor.getHtml()
|
|
console.log('editor content', html)
|
|
// 将编辑内容发送到 WebSocket 服务器
|
|
}
|
|
}
|
|
|
|
const editor = createEditor({
|
|
selector: '#editor-container',
|
|
html: '<p><br></p>',
|
|
config: editorConfig,
|
|
mode: 'default', // or 'simple'
|
|
})
|
|
|
|
const toolbarConfig = {}
|
|
|
|
const toolbar = createToolbar({
|
|
editor,
|
|
selector: '#toolbar-container',
|
|
config: toolbarConfig,
|
|
mode: 'default', // or 'simple'
|
|
})
|
|
</script>
|
|
</body>
|
|
</html>
|