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

document postEditor.setRange and change edit methods to use it #258

Closed
bantic opened this issue Dec 8, 2015 · 4 comments · Fixed by #446
Closed

document postEditor.setRange and change edit methods to use it #258

bantic opened this issue Dec 8, 2015 · 4 comments · Fixed by #446

Comments

@bantic
Copy link
Collaborator

bantic commented Dec 8, 2015

#248 added postEditor.setRange (and editor.selectRange). More of the edit methods in postEditor should be updated to use setRange where applicable, and write documentation to describe how to use this API

@mixonic
Copy link
Contributor

mixonic commented Feb 8, 2016

Things that need to be figured out:

  • editor.cursor.clearSelection() How to migrate off
  • editor.cursor should have a deprecation message added. Cursor should become private API and no longer have offsets on it?
  • editor.cursor.hasCursor() How to migrate off

@rlivsey
Copy link
Collaborator

rlivsey commented Feb 9, 2016

Some way of getting the current cursor position & setting it again later would be useful if cursor itself is to become private.

I'm currently using the following functions to stash & restore a cursor position when a document is updated externally:

function stashCursor(editor) {
  if (!editor || !editor.cursor.hasCursor()) {
    return;
  }

  const { offset, section } = editor.cursor.offsets.head;
  const index = editor.post.sections.toArray().indexOf(section);
  return { index, offset };
}

function reapplyCursor(editor, selection) {
  if (!editor || !selection) {
    return;
  }

  const { index, offset } = selection;

  const section = editor.post.sections.objectAt(index);
  if (!section) {
    return;
  }

  const position = new Position(section, offset);
  const range    = new Range(position, position);

  editor.cursor.selectRange(range);

  editor._reportSelectionState(); // TODO - should we have to do this?
}

@mixonic
Copy link
Contributor

mixonic commented Feb 9, 2016

@rlivsey so what we're doing is moving away from editor.cursor.offsets to a new API, editor.range.

editor.cursor.offsets always reads the cursor position from actual DOM. Once you've done something to modify the abstract post, that position may no longer be coupled to any existing section.

editor.range, as you set it, memoizes the range. So after setting, if you ask you'll get back the stored range. This allows us to change the range (and query it) several times during postEditing and flush the position once after rendering.

Some APIs:

So we're definitely going to keep a cursor range, but it should settle around editor.range and not the cursor API. Still questions to answer though.

bantic added a commit that referenced this issue Aug 3, 2016
bantic added a commit that referenced this issue Aug 3, 2016
Allow `Editor#selectRange`, `postEditor#setRange` to receive Range *or* Position
Add `Position#toRange`, `Section#toRange` and `Post#toRange`
Add `toRange` util to convert range|position into range
Remove unused `Position#clone`
Add `section#toPosition(offset)`
Expose `Position` on global MobiledocKit export
docs: label `postEditor#setRange` public fixes #258
bantic added a commit that referenced this issue Aug 3, 2016
Allow `Editor#selectRange`, `postEditor#setRange` to receive Range *or* Position
Add `Position#toRange`, `Section#toRange` and `Post#toRange`
Add `toRange` util to convert range|position into range
Remove unused `Position#clone`
Add `section#toPosition(offset)`
Expose `Position` on global MobiledocKit export
docs: label `postEditor#setRange` public fixes #258
bantic added a commit that referenced this issue Aug 3, 2016
  * Allow `Editor#selectRange`, `postEditor#setRange`,
  * `postEditor#toggleSection`, and `postEditor#toggleMarkup` to receive
  * Range *or* Position
  * Add `Position#toRange`, `Section#toRange` and `Post#toRange` methods
  * Add `toRange` util to convert range|position into range
  * Remove unused `Position#clone`, `Range.fromSection`
  * Add `section#toPosition(offset)`
  * Expose `Position` on global MobiledocKit export
  * docs: label `postEditor#setRange` public fixes #258
@bantic
Copy link
Collaborator Author

bantic commented Aug 4, 2016

released in v0.10.4

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

Successfully merging a pull request may close this issue.

3 participants