@ckeditor/ckeditor5-typing 45.2.1-alpha.4 → 45.2.1-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@
4
4
  */
5
5
  import { Command, Plugin } from '@ckeditor/ckeditor5-core/dist/index.js';
6
6
  import { env, EventInfo, count, isInsideSurrogatePair, isInsideCombinedSymbol, isInsideEmojiSequence, keyCodes, ObservableMixin } from '@ckeditor/ckeditor5-utils/dist/index.js';
7
- import { Observer, FocusObserver, DomEventData, LiveRange, BubblingEventInfo, MouseObserver, TouchObserver } from '@ckeditor/ckeditor5-engine/dist/index.js';
7
+ import { Observer, FocusObserver, DomEventData, _tryFixingModelRange, LiveRange, BubblingEventInfo, MouseObserver, TouchObserver } from '@ckeditor/ckeditor5-engine/dist/index.js';
8
8
  import { debounce, escapeRegExp } from 'es-toolkit/compat';
9
9
 
10
10
  /**
@@ -359,6 +359,23 @@ const TYPING_INPUT_TYPES_ANDROID = [
359
359
  if (view.document.selection.isFake && viewSelection && view.document.selection.isSimilar(viewSelection)) {
360
360
  data.preventDefault();
361
361
  }
362
+ // In case of typing on a non-collapsed range, we have to handle it ourselves as a browser
363
+ // could modify the DOM unpredictably.
364
+ // Noticed cases:
365
+ // * <pre><code>[foo</code></pre><p>]bar</p>
366
+ // * <p>[foo</p><pre>]<code>bar</code></pre>
367
+ // * <p>[foo</p><blockquote><p>]bar</p></blockquote>
368
+ //
369
+ // Especially tricky case is when a code block follows a paragraph as code block on the view side
370
+ // is rendered as a <code> element inside a <pre> element, but only the <code> element is mapped to the model.
371
+ // While mapping view position <pre>]<code> to model, the model position results before the <codeBlock> element,
372
+ // and this triggers selection fixer to cover only text in the previous paragraph.
373
+ //
374
+ // This is safe for composition as those events are not cancellable
375
+ // and the preventDefault() and defaultPrevented are not affected.
376
+ if (viewSelection && Array.from(viewSelection.getRanges()).some((range)=>!range.isCollapsed)) {
377
+ data.preventDefault();
378
+ }
362
379
  if (!insertTextCommand.isEnabled) {
363
380
  // @if CK_DEBUG_TYPING // if ( ( window as any ).logCKETyping ) {
364
381
  // @if CK_DEBUG_TYPING // console.log( ..._buildLogMessage( this, 'Input',
@@ -372,7 +389,7 @@ const TYPING_INPUT_TYPES_ANDROID = [
372
389
  let modelRanges;
373
390
  // If view selection was specified, translate it to model selection.
374
391
  if (viewSelection) {
375
- modelRanges = Array.from(viewSelection.getRanges()).map((viewRange)=>mapper.toModelRange(viewRange));
392
+ modelRanges = Array.from(viewSelection.getRanges()).map((viewRange)=>mapper.toModelRange(viewRange)).map((modelRange)=>_tryFixingModelRange(modelRange, model.schema) || modelRange);
376
393
  } else {
377
394
  modelRanges = Array.from(modelSelection.getRanges());
378
395
  }
@@ -1224,9 +1241,7 @@ const DELETE_EVENT_TYPES = {
1224
1241
  sequence
1225
1242
  };
1226
1243
  if (unit == 'selection') {
1227
- const modelRanges = Array.from(selectionToRemove.getRanges()).map((viewRange)=>{
1228
- return editor.editing.mapper.toModelRange(viewRange);
1229
- });
1244
+ const modelRanges = Array.from(selectionToRemove.getRanges()).map((viewRange)=>editor.editing.mapper.toModelRange(viewRange)).map((modelRange)=>_tryFixingModelRange(modelRange, editor.model.schema) || modelRange);
1230
1245
  commandData.selection = editor.model.createSelection(modelRanges);
1231
1246
  } else {
1232
1247
  commandData.unit = unit;