Skip to content

Conversation

@bantic
Copy link
Collaborator

@bantic bantic commented Jul 28, 2016

Previously, the code path the editor took when deleting a single character (via postEditor#deleteFrom) and deleting a non-collapsed range (via postEditor#deleteRange) differed greatly. This PR deprecates deleteFrom and introduces postEditor#deleteAtPosition (and Editor#deleteAtPosition). These methods almost always delegate their work to deleteRange. This allows for much simplification of the deletion code, and the improved test coverage will enable future refactorings.

deleteAtPosition expands the position by 1 unit ("char" or "word") in direction (into a Range) and deletes that range. The only exception is when the position is at the head of a list item, in which case the list item is toggled into a markup section instead.

API Changes:

  • Simplifies deletion code in Editor class. Adds Editor#deleteAtPosition.
  • Deprecates postEditor#deleteFrom.
  • Remove now-unused postEditor#sectionsContainedBy
  • Removes several private helper methods that supported postEditor#deleteFrom

Test changes:

  • Adds many more tests for postEditor#deleteRange, adds postEditor#deleteAtPosition.
  • Removes tests for postEditor#deleteFrom
  • Adds test helpers for postEditor: MockEditor, run and renderBuiltAbstract, DRYs tests to share these abstractions
  • Adds test helper Helpers.postAbstract.buildFromText. This allows creating a post and range using a text-based DSL instead of the PostNodeBuilder (see notes below)

buildFromText DSL

/**
 * Shorthand to create a mobiledoc simply.
 * Pass a string or an array of strings.
 * 
 * Returns { post, range }, a post built from the mobiledoc and a range.
 *
 * Use "|" to indicate the cursor position or "<" and ">" to indicate a range.
 * Use "[card-name]" to indicate a card
 * Use asterisks to indicate bold text: "abc *bold* def"
 * Use "@" to indicate an atom
 * Use "* " at the start of the string to indicate a list item ("ul")
 *
 * Examples:
 * buildFromText("abc") -> { post } with 1 markup section ("p") with text "abc"
 * buildFromText(["abc","def"]) -> { post } with 2 markups sections ("p") with texts "abc" and "def"
 * buildFromText("abc|def") -> { post, range } where range is collapsed at offset 3 (after the "c")
 * buildFromText(["abcdef","[some-card]","def"]) -> { post } with [MarkupSection, Card, MarkupSection] sections
 * buildFromText(["* item 1", "* item 2"]) -> { post } with a ListSection with 2 ListItems
 * buildFromText(["<abc", "def", "ghi>"]) -> { post, range } where range is the entire post (before the "a" to after the "i")
 */

@bantic bantic changed the title wip add postEditor#deleteAtPosition, better tests for #deleteRange Refactor postEditor#deleteRange, deprecate #deleteFrom in favor of deleteAtPosition Aug 2, 2016
@bantic bantic merged commit 93a5709 into master Aug 3, 2016
@bantic bantic deleted the fix-delete-from branch August 3, 2016 15:41
@bantic bantic mentioned this pull request Aug 3, 2016
@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 this pull request may close these issues.

2 participants