Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Awareness of two quill instance on the same page #5

Open
csbenjamin opened this issue Aug 5, 2020 · 1 comment
Open

Awareness of two quill instance on the same page #5

csbenjamin opened this issue Aug 5, 2020 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@csbenjamin
Copy link

Is your feature request related to a problem? Please describe.
I want to have multiple instance of quill editors on a single page. Currently, if I want to use cursors, I need to use multiple providers (one for each quill instance), because all quill instances will try to use the same field ('cursor') on the awareness state.

Describe the solution you'd like
Being able to choose another fields from awareness state instead 'cursor' would be enough. For example, if I have two instances of quill editors on my page, I could set 'cursor1' for the first instance and 'cursor2' for the second one.

@csbenjamin csbenjamin added the enhancement New feature or request label Aug 5, 2020
@gustavotoyota
Copy link

I solved this problem with these changes:

diff --git a/node_modules/y-quill/src/y-quill.js b/node_modules/y-quill/src/y-quill.js
index e668115..684f889 100644
--- a/node_modules/y-quill/src/y-quill.js
+++ b/node_modules/y-quill/src/y-quill.js
@@ -122,11 +122,7 @@ export class QuillBinding {
       if (awareness && quillCursors) {
         const sel = quill.getSelection()
         const aw = /** @type {any} */ (awareness.getLocalState())
-        if (sel === null) {
-          if (awareness.getLocalState() !== null) {
-            awareness.setLocalStateField('cursor', /** @type {any} */ (null))
-          }
-        } else {
+        if (sel !== null) {
           const anchor = Y.createRelativePositionFromTypeIndex(type, sel.index)
           const head = Y.createRelativePositionFromTypeIndex(type, sel.index + sel.length)
           if (!aw || !aw.cursor || !Y.compareRelativePositions(anchor, aw.cursor.anchor) || !Y.compareRelativePositions(head, aw.cursor.head)) {
@@ -155,6 +151,16 @@ export class QuillBinding {
       })
       awareness.on('change', this._awarenessChange)
     }
+
+    let focused = false
+    quill.on('selection-change', (range) => {
+      if (range) {
+        focused = true
+      } else if (focused) {
+        focused = false
+        awareness.setLocalStateField('cursor', null)
+      }
+    })
   }
   destroy () {
     this.type.unobserve(this._typeObserver)

This works for 0.1.4.
I'm having problems with 0.1.5 so I don't know about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants