-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue of incorrect display of remote selection in Quill example (#…
…769) This commit addresses the issue of the remote cursor not being accurately displayed when local edits are made in the Quill example. It ensures that the remote cursor is correctly rendered after each edit.(Ref: y-quill) It also adds extra checks to update the selection when there are differences between the selection in Yorkie and Quill. For example, if you add text before another user's selected text, Quill might include the added text in the remote-selection. In such cases, it is necessary to update the Quill selection in Yorkie.
- Loading branch information
Showing
6 changed files
with
103 additions
and
73 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
<script src="https://cdn.quilljs.com/1.3.6/quill.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/quill-cursors.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/color-hash.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/short-unique-id.min.js"></script> | ||
</head> | ||
<body> | ||
<div id="network-status"></div> | ||
|
@@ -28,7 +27,6 @@ | |
const documentElem = document.getElementById('document'); | ||
const documentTextElem = document.getElementById('document-text'); | ||
const networkStatusElem = document.getElementById('network-status'); | ||
const shortUniqueID = new ShortUniqueId(); | ||
const colorHash = new ColorHash(); | ||
const documentKey = 'quill'; | ||
|
||
|
@@ -51,15 +49,16 @@ | |
} | ||
|
||
function displayOnlineClients(presences, myClientID) { | ||
const usernames = []; | ||
const clients = []; | ||
for (const { clientID, presence } of presences) { | ||
usernames.push( | ||
myClientID === clientID | ||
? `<b>${presence.username}</b>` | ||
: presence.username, | ||
); | ||
const clientElem = `<span class="client" style='background: ${presence.color}; color: white; margin-right:2px; padding:2px;'>${presence.name}</span>`; | ||
if (myClientID === clientID) { | ||
clients.unshift(clientElem); | ||
continue; | ||
} | ||
clients.push(clientElem); | ||
} | ||
onlineClientsElem.innerHTML = JSON.stringify(usernames); | ||
onlineClientsElem.innerHTML = clients.join(''); | ||
} | ||
|
||
async function main() { | ||
|
@@ -77,7 +76,10 @@ | |
}); | ||
|
||
await client.attach(doc, { | ||
initialPresence: { username: `username-${shortUniqueID()}` }, | ||
initialPresence: { | ||
name: client.getID().slice(-2), | ||
color: colorHash.hex(client.getID().slice(-2)), | ||
}, | ||
}); | ||
|
||
doc.update((root) => { | ||
|
@@ -101,12 +103,14 @@ | |
const { actor, message, operations } = event.value; | ||
handleOperations(operations, actor); | ||
} | ||
updateAllCursors(); | ||
}); | ||
|
||
doc.subscribe('others', (event) => { | ||
if (event.type === 'unwatched') { | ||
cursors.removeCursor(event.value.presence.username); | ||
cursors.removeCursor(event.value.clientID); | ||
} else if (event.type === 'presence-changed') { | ||
displayRemoteCursor(event.value); | ||
updateCursor(event.value); | ||
} | ||
}); | ||
|
||
|
@@ -138,20 +142,29 @@ | |
}); | ||
const cursors = quill.getModule('cursors'); | ||
|
||
function displayRemoteCursor(user) { | ||
const { | ||
clientID: id, | ||
presence: { username, selection }, | ||
} = user; | ||
if (!selection || id === client.getID()) return; | ||
function updateCursor(user) { | ||
const { clientID, presence } = user; | ||
if (clientID === client.getID()) return; | ||
// TODO(chacha912): After resolving the presence initialization issue(#608), | ||
// remove the following check. | ||
if (!presence) return; | ||
|
||
const { name, color, selection } = presence; | ||
if (!selection) return; | ||
const range = doc.getRoot().content.posRangeToIndexRange(selection); | ||
cursors.createCursor(username, username, colorHash.hex(username)); | ||
cursors.moveCursor(username, { | ||
cursors.createCursor(clientID, name, color); | ||
cursors.moveCursor(clientID, { | ||
index: range[0], | ||
length: range[1] - range[0], | ||
}); | ||
} | ||
|
||
function updateAllCursors() { | ||
for (const user of doc.getPresences()) { | ||
updateCursor(user); | ||
} | ||
} | ||
|
||
// 04. bind the document with the Quill. | ||
// 04-1. Quill to Document. | ||
quill | ||
|
@@ -231,9 +244,22 @@ | |
} | ||
}) | ||
.on('selection-change', (range, _, source) => { | ||
if (source === 'api' || !range) { | ||
if (!range) { | ||
return; | ||
} | ||
// NOTE(chacha912): If the selection in the Quill editor does not match the range computed by yorkie, | ||
// additional updates are necessary. This condition addresses situations where Quill's selection behaves | ||
// differently, such as when inserting text before a range selection made by another user, causing | ||
// the second character onwards to be included in the selection. | ||
if (source === 'api') { | ||
const [from, to] = doc | ||
.getRoot() | ||
.content.posRangeToIndexRange(doc.getMyPresence().selection); | ||
const { index, length } = range; | ||
if (from === index && to === index + length) { | ||
return; | ||
} | ||
} | ||
|
||
doc.update((root, presence) => { | ||
presence.set({ | ||
|
@@ -250,7 +276,6 @@ | |
const deltaOperations = []; | ||
let prevTo = 0; | ||
for (const op of ops) { | ||
const actorName = doc.getPresence(actor).username; | ||
const from = op.from; | ||
const to = op.to; | ||
const retainFrom = from - prevTo; | ||
|
@@ -318,10 +343,8 @@ | |
} | ||
|
||
syncText(); | ||
updateAllCursors(); | ||
displayLog(documentElem, documentTextElem, doc); | ||
for (const user of doc.getPresences()) { | ||
displayRemoteCursor(user); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
|
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