Skip to content

Commit 18be7d0

Browse files
PavlovicWorkConeSpeccgohaberegtasuku-sathul7744
authored
Fix for input and textarea bug (#1214)
* [Improvements] ESLint action (#1099) * TSLint -> ESLint, GitHub Action * Update eslint.yml * Autofix * more autofix * fix * manually fix some issues * Update CHANGELOG.md * [Refactor] ESLint fixed (#1100) Co-authored-by: Peter Savchenko <specc.dev@gmail.com> * [Feature] i18n (#1106) * i18n first steps * i18n internal, toolbox, api for tools * namespaced api * tn, t * tn in block tunes * join toolbox and inlineTools under toolNames * translations * make enum toolTypes * Update block.ts * Update src/components/core.ts Co-Authored-By: George Berezhnoy <gohabereg@users.noreply.github.com> * add more types * rm tn * export i18n types * upd bundle * fix tabulation * Add type-safe namespaces * upd * Improve example * Update toolbox.ts * improve examplle * upd * fix typo * Add comments for complex types Co-authored-by: George Berezhnoy <gohabereg@users.noreply.github.com> Co-authored-by: Georgy Berezhnoy <gohabereg@gmail.com> * Remove unused submodule * Fixed: icon centering in Firefox * Do not load styles twice (#1112) * Do not load styles twice * Add changelog * Fix issue link Co-authored-by: Peter Savchenko <specc.dev@gmail.com> * Show warning if Block to delete is not found (#1111) Resolves #1102 * Save Tools' order in the Toolbox (#1113) Resolves #1073 * fix $.isEmpty performance (#1096) * fix $.isEmpty performance * add changelog * upd bundle Co-authored-by: Peter Savchenko <specc.dev@gmail.com> * Add issue templates (#1114) * Update issue templates (#1121) * Update issue templates * Apply suggestions from code review Co-Authored-By: George Berezhnoy <gohabereg@users.noreply.github.com> * upd texts * Update feature_request.md * Update .github/ISSUE_TEMPLATE/discussion.md Co-Authored-By: George Berezhnoy <gohabereg@users.noreply.github.com> Co-authored-by: George Berezhnoy <gohabereg@users.noreply.github.com> * Allowing deleting block by block id (#1108) * Allowing deleting block by block id * Fixed no argument error * Making index value optional for delete operation * Added to changelog * Making index value optional for delete operation * Added parameter description * Update docs/CHANGELOG.md * Update types/api/blocks.d.ts * Update editor.js Co-authored-by: Peter Savchenko <specc.dev@gmail.com> * Allow navigate next from last non-initial block (#1110) Resolves #1103 * Create CODE_OF_CONDUCT.md (#1171) * Create CODE_OF_CONDUCT.md * Update changelog file * Update dependencies (#1122) * Update dependencies * upd codex.tooltip * Update editor.js.LICENSE.txt Co-authored-by: Peter Savchenko <specc.dev@gmail.com> * Feature/disable tab event config (#1164) * Highlight first block on autofocus (#1127) * Fix shortcut for external tools (#1141) * fix/shortcut-for-external-tools * Check inline tools property for shortcut Co-authored-by: George Berezhnoy <gohabereg@gmail.com> * Hotfix/issue1133 selection shortcut removed on editor destroy (#1140) * Removed shortcut CMD+A on editor destroy #1133 * Removed patch version and made code cleaner #1133 * lint error fixes #1133 Co-authored-by: Sisir <sisir@hellosivi.com> Co-authored-by: George Berezhnoy <gohabereg@gmail.com> * [Feature] BlockAPI Interface (#1075) * Fix BlockManager.insert method (#1172) * Fix BlockManager.insert method * upd * Explicitly check for undefined * Update tools master branches (#1180) * Update master branches * Update image * Update CHANGELOG.md * Fix behaviour of inputs editing in block settings (#1123) * lint code * Update CHANGELOG.md * added handling of inputs and textareas in custom plugins * Upd tools * Add changelog * Upd submodules Co-authored-by: Peter Savchenko <specc.dev@gmail.com> Co-authored-by: George Berezhnoy <gohabereg@users.noreply.github.com> Co-authored-by: Georgy Berezhnoy <gohabereg@gmail.com> Co-authored-by: tasuku-s <tasuku@freemind.co.jp> Co-authored-by: Athul Anil Kumar <athul7744@outlook.com> Co-authored-by: Taly <vitalik7tv@yandex.ru> Co-authored-by: flaming-cl <51183663+flaming-cl@users.noreply.github.com> Co-authored-by: Nguyen Ngoc Son <sonnn.se@gmail.com> Co-authored-by: Sisir Das K <37764463+sis-dk@users.noreply.github.com> Co-authored-by: Sisir <sisir@hellosivi.com>
1 parent 08221c7 commit 18be7d0

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

dist/editor.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- `Fix` - Tool's `destroy` method is not invoked when `editor.destroy()` is called. [#1047](https://github.com/codex-team/editor.js/issues/1047)
1212
- `New` - Tool's `reset` static method added to the API to clean up any data added by Tool on initialization
1313
- `Improvements` - Unuseful log about missed i18n section has been removed [#1269](https://github.com/codex-team/editor.js/issues/1269)
14+
- `Fix` - Fixed issue with enter key in inputs and textareas [#920](https://github.com/codex-team/editor.js/issues/920)
1415

1516
### 2.18
1617

src/components/block/index.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ export default class Block {
621621
* Update current input index with selection anchor node
622622
*/
623623
public updateCurrentInput(): void {
624-
this.currentInput = SelectionUtils.anchorNode;
624+
this.currentInput = document.activeElement;
625625
}
626626

627627
/**
@@ -640,13 +640,20 @@ export default class Block {
640640
attributes: true,
641641
}
642642
);
643+
644+
/**
645+
* Mutation observer doesn't track changes in "<input>" and "<textarea>"
646+
* so we need to track focus events
647+
*/
648+
this.addInputEvents();
643649
}
644650

645651
/**
646652
* Is fired when Block will be unselected
647653
*/
648654
public willUnselect(): void {
649655
this.mutationObserver.disconnect();
656+
this.removeInputEvents();
650657
}
651658

652659
/**
@@ -664,4 +671,37 @@ export default class Block {
664671

665672
return wrapper;
666673
}
674+
675+
/**
676+
* Is fired when text input or contentEditable is focused
677+
*/
678+
private handleFocus = (): void => {
679+
/**
680+
* Drop cache
681+
*/
682+
this.cachedInputs = [];
683+
684+
/**
685+
* Update current input
686+
*/
687+
this.updateCurrentInput();
688+
}
689+
690+
/**
691+
* Adds focus event listeners to all inputs and contentEditables
692+
*/
693+
private addInputEvents(): void {
694+
this.inputs.forEach(input => {
695+
input.addEventListener('focus', this.handleFocus);
696+
});
697+
}
698+
699+
/**
700+
* removes focus event listeners from all inputs and contentEditables
701+
*/
702+
private removeInputEvents(): void {
703+
this.inputs.forEach(input => {
704+
input.removeEventListener('focus', this.handleFocus);
705+
});
706+
}
667707
}

src/components/modules/caret.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,30 @@ export default class Caret extends Module {
355355
selectRange.deleteContents();
356356

357357
if (currentBlockInput) {
358-
const range = selectRange.cloneRange();
358+
if ($.isNativeInput(currentBlockInput)) {
359+
/**
360+
* If input is native text input we need to use it's value
361+
* Text before the caret stays in the input,
362+
* while text after the caret is returned as a fragment to be inserted after the block.
363+
*/
364+
const input = currentBlockInput as HTMLInputElement | HTMLTextAreaElement;
365+
const newFragment = document.createDocumentFragment();
359366

360-
range.selectNodeContents(currentBlockInput);
361-
range.setStart(selectRange.endContainer, selectRange.endOffset);
367+
const inputRemainingText = input.value.substring(0, input.selectionStart);
368+
const fragmentText = input.value.substring(input.selectionStart);
362369

363-
return range.extractContents();
370+
newFragment.textContent = fragmentText;
371+
input.value = inputRemainingText;
372+
373+
return newFragment;
374+
} else {
375+
const range = selectRange.cloneRange();
376+
377+
range.selectNodeContents(currentBlockInput);
378+
range.setStart(selectRange.endContainer, selectRange.endOffset);
379+
380+
return range.extractContents();
381+
}
364382
}
365383
}
366384
}

0 commit comments

Comments
 (0)