Skip to content

Commit

Permalink
[Fix]: issue with leading new lines (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
gohabereg authored Jun 30, 2019
1 parent e6c5753 commit 979d51b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
8 changes: 4 additions & 4 deletions dist/editor.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- `Improvements` — Inline Toolbar now works on mobile devices [#706](https://github.com/codex-team/editor.js/issues/706)
- `Improvements` — Toolbar looks better on mobile devices [#706](https://github.com/codex-team/editor.js/issues/706)
- `Fix` — Resolve bug with deleting leading new lines [#726](https://github.com/codex-team/editor.js/issues/726)
- `Fix` — Fix inline link Tool to support different link types like `mailto` and `tel` [#809](https://github.com/codex-team/editor.js/issues/809)
- `Fix` — Added `typeof` util method to check exact object type [#805](https://github.com/codex-team/editor.js/issues/805)
- `New` *Conversion Toolbar* — Ability to convert one block to another [#704](https://github.com/codex-team/editor.js/issues/704)
Expand Down
24 changes: 22 additions & 2 deletions src/components/modules/caret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,21 @@ export default class Caret extends Module {
*/
if ($.isLineBreakTag(firstNode as HTMLElement) || $.isEmpty(firstNode)) {
const leftSiblings = this.getHigherLevelSiblings(anchorNode as HTMLElement, 'left');
const nothingAtLeft = leftSiblings.every((node, i) => $.isEmpty(node));
const nothingAtLeft = leftSiblings.every((node) => {
/**
* Workaround case when block starts with several <br>'s (created by SHIFT+ENTER)
* @see https://github.com/codex-team/editor.js/issues/726
* We need to allow to delete such linebreaks, so in this case caret IS NOT AT START
*/
const regularLineBreak = $.isLineBreakTag(node);
/**
* Workaround SHIFT+ENTER in Safari, that creates <div><br></div> instead of <br>
*/
const lineBreakInSafari = node.children.length === 1 && $.isLineBreakTag(node.children[0] as HTMLElement);
const isLineBreak = regularLineBreak || lineBreakInSafari;

return $.isEmpty(node) && !isLineBreak;
});

if (nothingAtLeft && anchorOffset === firstLetterPosition) {
return true;
Expand Down Expand Up @@ -180,8 +194,14 @@ export default class Caret extends Module {
*/
if ($.isLineBreakTag(lastNode as HTMLElement) || $.isEmpty(lastNode)) {
const rightSiblings = this.getHigherLevelSiblings(anchorNode as HTMLElement, 'right');

const nothingAtRight = rightSiblings.every((node, i) => {
return i === 0 && $.isLineBreakTag(node as HTMLElement) || $.isEmpty(node);
/**
* If last right sibling is BR isEmpty returns false, but there actually nothing at right
*/
const isLastBR = i === rightSiblings.length - 1 && $.isLineBreakTag(node as HTMLElement);

return (isLastBR) || $.isEmpty(node) && !$.isLineBreakTag(node);
});

if (nothingAtRight && anchorOffset === anchorNode.textContent.length) {
Expand Down

0 comments on commit 979d51b

Please sign in to comment.