更新最新代码
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<html lang="en">
|
||||
<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>
|
||||
<meta charset="UTF-8">
|
||||
<title>协同编辑器</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="editor—wrapper">
|
||||
<div id="toolbar-container"><!-- 工具栏 --></div>
|
||||
<div id="editor-container"><!-- 编辑器 --></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
<script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
|
||||
<script>
|
||||
const { createEditor, createToolbar } = window.wangEditor
|
||||
|
||||
const editorConfig = {
|
||||
placeholder: 'Type here...',
|
||||
onChange(editor) {
|
||||
const html = editor.getHtml()
|
||||
console.log('editor content', html)
|
||||
// 也可以同步到 <textarea>
|
||||
}
|
||||
}
|
||||
|
||||
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>
|
||||
@@ -0,0 +1,28 @@
|
||||
import asyncio
|
||||
import websockets
|
||||
import json
|
||||
|
||||
connected = set()
|
||||
|
||||
async def handler(websocket):
|
||||
# 当新的客户端连接时,添加到连接集合
|
||||
connected.add(websocket)
|
||||
try:
|
||||
async for message in websocket:
|
||||
data = json.loads(message)
|
||||
if data['type'] == 'edit':
|
||||
# 广播编辑消息到所有连接的WebSocket
|
||||
await broadcast(data['content'])
|
||||
finally:
|
||||
# 当客户端断开连接时,移除
|
||||
connected.remove(websocket)
|
||||
|
||||
async def broadcast(message):
|
||||
# 往所有客户端广播消息
|
||||
for ws in connected:
|
||||
await ws.send(message)
|
||||
|
||||
start_server = websockets.serve(handler, "localhost", 6789)
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(start_server)
|
||||
asyncio.get_event_loop().run_forever()
|
||||
@@ -0,0 +1,22 @@
|
||||
import asyncio
|
||||
import websockets
|
||||
|
||||
# 存储所有连接的客户端
|
||||
connected_clients = set()
|
||||
|
||||
async def broadcast(message):
|
||||
if connected_clients:
|
||||
await asyncio.wait([client.send(message) for client in connected_clients])
|
||||
|
||||
async def handler(websocket, path):
|
||||
connected_clients.add(websocket)
|
||||
try:
|
||||
async for message in websocket:
|
||||
await broadcast(message)
|
||||
finally:
|
||||
connected_clients.discard(websocket)
|
||||
|
||||
start_server = websockets.serve(handler, "localhost", 8765)
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(start_server)
|
||||
asyncio.get_event_loop().run_forever()
|
||||
@@ -0,0 +1,80 @@
|
||||
<!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>
|
||||
@@ -0,0 +1,49 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Collaborative Editing</title>
|
||||
<style>
|
||||
#editor {
|
||||
width: 100%;
|
||||
height: 400px;
|
||||
border: 1px solid #ccc;
|
||||
padding: 10px;
|
||||
box-sizing: border-box;
|
||||
font-family: monospace;
|
||||
}
|
||||
</style>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', (event) => {
|
||||
const ws = new WebSocket('ws://localhost:6789');
|
||||
const editor = document.getElementById('editor');
|
||||
|
||||
ws.onopen = function(event) {
|
||||
console.log('Connection established!');
|
||||
};
|
||||
|
||||
ws.onmessage = function(event) {
|
||||
// 当接收到消息时,更新编辑器的内容
|
||||
const message = JSON.parse(event.data);
|
||||
editor.value = message; // 假设消息直接是编辑内容
|
||||
};
|
||||
|
||||
ws.onclose = function(event) {
|
||||
console.log('Connection closed!');
|
||||
};
|
||||
|
||||
editor.oninput = function(event) {
|
||||
// 当编辑器内容发生变化时,发送消息
|
||||
const message = {
|
||||
type: 'edit',
|
||||
content: editor.value
|
||||
};
|
||||
ws.send(JSON.stringify(message));
|
||||
};
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<textarea id="editor" placeholder="Start editing..."></textarea>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,39 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>富文本协同编辑器</title>
|
||||
<link rel="stylesheet" href="https://cdn.quilljs.com/1.3.6/quill.core.css">
|
||||
<link rel="stylesheet" href="https://cdn.quilljs.com/1.3.6/quill.snow.css">
|
||||
<script src="https://cdn.quilljs.com/1.3.6/quill.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.0/socket.io.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div id="editor" class="ql-container ql-snow"></div>
|
||||
|
||||
<script>
|
||||
var quill = new Quill('#editor', {
|
||||
modules: {
|
||||
toolbar: [
|
||||
['bold', 'italic', 'underline'],
|
||||
[{'list': 'ordered'}, {'list': 'bullet'}]
|
||||
]
|
||||
},
|
||||
theme: 'snow'
|
||||
});
|
||||
|
||||
var socket = io('http://localhost:6789');
|
||||
|
||||
quill.on('text-change', function(delta, oldDelta, source) {
|
||||
|
||||
socket.emit('editor-change', delta);
|
||||
});
|
||||
|
||||
socket.on('broadcast-editor-change', function(delta) {
|
||||
quill.updateContents(delta);
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user