lakelib 0.1.20 → 0.1.22

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/lib/lake.js CHANGED
@@ -676,14 +676,6 @@ class Nodes {
676
676
  }
677
677
  return true;
678
678
  }
679
- // Returns a boolean value indicating whether the node and the target node are siblings.
680
- isSibling(target) {
681
- if (this.length === 0) {
682
- return false;
683
- }
684
- const parent = this.get(0).parentNode;
685
- return parent && parent === target.parent().get(0);
686
- }
687
679
  // Gets a native node at the specified index.
688
680
  get(index) {
689
681
  return this.nodeList[index];
@@ -732,6 +724,23 @@ class Nodes {
732
724
  const element = this.get(0);
733
725
  return element.matches(selector);
734
726
  }
727
+ // Returns a boolean value indicating whether a node is a descendant of a given node, that is the node itself,
728
+ // one of its direct children (childNodes), one of the children's direct children, and so on.
729
+ contains(otherNode) {
730
+ if (otherNode instanceof Nodes) {
731
+ otherNode = otherNode.get(0);
732
+ }
733
+ const element = this.get(0);
734
+ return element.contains(otherNode);
735
+ }
736
+ // Returns a boolean value indicating whether the node and the target node are siblings.
737
+ isSibling(target) {
738
+ if (this.length === 0) {
739
+ return false;
740
+ }
741
+ const parent = this.get(0).parentNode;
742
+ return parent && parent === target.parent().get(0);
743
+ }
735
744
  // Returns the descendants of the first element which are selected by the specified CSS selector.
736
745
  find(selector) {
737
746
  if (typeof selector === 'string') {
@@ -5164,7 +5173,7 @@ function removeBox(range) {
5164
5173
  return box;
5165
5174
  }
5166
5175
 
5167
- var version = "0.1.20";
5176
+ var version = "0.1.22";
5168
5177
 
5169
5178
  // Returns the attributes of the element as an key-value object.
5170
5179
  function getAttributes(node) {
@@ -5256,6 +5265,9 @@ class Selection {
5256
5265
  // Updates the saved range with the range of the native selection.
5257
5266
  updateByRange() {
5258
5267
  const newRange = this.getRangeFromNativeSelection();
5268
+ if (!this.container.contains(newRange.commonAncestor)) {
5269
+ return;
5270
+ }
5259
5271
  if (this.range.startNode.get(0) === newRange.startNode.get(0) &&
5260
5272
  this.range.startOffset === newRange.startOffset &&
5261
5273
  this.range.endNode.get(0) === newRange.endNode.get(0) &&
@@ -5494,8 +5506,7 @@ class History {
5494
5506
  const box = getBox(nativeNode);
5495
5507
  box.getContainer().empty();
5496
5508
  });
5497
- if (range.commonAncestor.isOutside ||
5498
- range.commonAncestor.closestContainer().get(0) !== this.container.get(0)) {
5509
+ if (!this.container.contains(range.commonAncestor)) {
5499
5510
  return newContainer;
5500
5511
  }
5501
5512
  if (range.isInsideBox) {
@@ -5754,27 +5765,27 @@ class Editor {
5754
5765
  this.box = Editor.box;
5755
5766
  this.copyListener = event => {
5756
5767
  const range = this.selection.range;
5757
- if (range.commonAncestor.closestContainer().get(0) !== this.container.get(0)) {
5768
+ if (!this.container.contains(range.commonAncestor)) {
5758
5769
  return;
5759
5770
  }
5760
5771
  this.event.emit('copy', event);
5761
5772
  };
5762
5773
  this.cutListener = event => {
5763
5774
  const range = this.selection.range;
5764
- if (range.commonAncestor.closestContainer().get(0) !== this.container.get(0)) {
5775
+ if (!this.container.contains(range.commonAncestor)) {
5765
5776
  return;
5766
5777
  }
5767
5778
  this.event.emit('cut', event);
5768
5779
  };
5769
5780
  this.pasteListener = event => {
5770
5781
  const range = this.selection.range;
5771
- if (range.commonAncestor.closestContainer().get(0) !== this.container.get(0)) {
5782
+ if (!this.container.contains(range.commonAncestor)) {
5772
5783
  return;
5773
5784
  }
5774
5785
  this.event.emit('paste', event);
5775
5786
  };
5776
5787
  this.selectionchangeListener = () => {
5777
- this.selection.updateByRange();
5788
+ this.updateSelectionRange();
5778
5789
  this.updateBoxSelectionStyle();
5779
5790
  this.emitStateChangeEvent();
5780
5791
  };
@@ -5788,6 +5799,11 @@ class Editor {
5788
5799
  this.resizeListener = () => {
5789
5800
  this.event.emit('resize');
5790
5801
  };
5802
+ this.updateSelectionRange = debounce(() => {
5803
+ this.selection.updateByRange();
5804
+ }, 1, {
5805
+ immediate: true,
5806
+ });
5791
5807
  // Updates the classes of all boxes when the current selection of the editor is changed.
5792
5808
  this.updateBoxSelectionStyle = debounce(() => {
5793
5809
  // The editor has been unmounted.
@@ -5840,8 +5856,7 @@ class Editor {
5840
5856
  this.emitStateChangeEvent = debounce(() => {
5841
5857
  const commandNames = this.command.getNames();
5842
5858
  let appliedItems = this.selection.getAppliedItems();
5843
- if (appliedItems.length > 0 &&
5844
- appliedItems[0].node.closestContainer().get(0) !== this.container.get(0)) {
5859
+ if (appliedItems.length > 0 && !this.container.contains(appliedItems[0].node)) {
5845
5860
  appliedItems = [];
5846
5861
  }
5847
5862
  const disabledNameMap = new Map();
@@ -5959,60 +5974,65 @@ class Editor {
5959
5974
  this.unsavedInputData = '';
5960
5975
  this.unsavedInputCount = 0;
5961
5976
  }
5962
- // Binds events about input.
5977
+ // Handles input event.
5978
+ handleInputEvent(event) {
5979
+ var _a;
5980
+ this.selection.updateByRange();
5981
+ const range = this.selection.range;
5982
+ if (range.isInsideBox) {
5983
+ return;
5984
+ }
5985
+ if (range.isBoxStart || range.isBoxEnd) {
5986
+ this.moveBoxStripText();
5987
+ this.history.save();
5988
+ return;
5989
+ }
5990
+ const inputType = event instanceof CompositionEvent ? 'insertText' : event.inputType;
5991
+ if (inputType === 'insertText') {
5992
+ const inputData = (_a = event.data) !== null && _a !== void 0 ? _a : '';
5993
+ if (inputData.length > 1) {
5994
+ this.history.save({
5995
+ inputType: 'insertText',
5996
+ update: false,
5997
+ });
5998
+ return;
5999
+ }
6000
+ this.unsavedInputData += inputData;
6001
+ this.unsavedInputCount++;
6002
+ if (this.unsavedInputData.length < this.config.minChangeSize) {
6003
+ this.history.save({
6004
+ inputType: 'insertText',
6005
+ update: this.unsavedInputCount > 1,
6006
+ });
6007
+ }
6008
+ else {
6009
+ this.history.save({
6010
+ inputType: 'insertText',
6011
+ update: true,
6012
+ });
6013
+ this.resetUnsavedInputData();
6014
+ }
6015
+ return;
6016
+ }
6017
+ this.history.save();
6018
+ }
6019
+ // Binds events about inputting text.
5963
6020
  bindInputEvents() {
5964
6021
  this.container.on('compositionstart', () => {
5965
6022
  this.isComposing = true;
6023
+ this.container.removeClass('lake-placeholder');
5966
6024
  });
5967
- this.container.on('compositionend', () => {
6025
+ this.container.on('compositionend', event => {
5968
6026
  this.isComposing = false;
6027
+ this.handleInputEvent(event);
5969
6028
  });
5970
6029
  this.container.on('input', event => {
5971
6030
  const inputEvent = event;
5972
- // Here setTimeout is necessary because isComposing is not false after ending composition.
5973
- window.setTimeout(() => {
5974
- var _a;
5975
- const range = this.selection.range;
5976
- if (range.isInsideBox) {
5977
- return;
5978
- }
5979
- // isComposing is false after ending composition because compositionend event has been emitted.
5980
- if (this.isComposing) {
5981
- if (inputEvent.inputType === 'insertCompositionText') {
5982
- this.container.removeClass('lake-placeholder');
5983
- }
5984
- this.event.emit('input', inputEvent);
5985
- return;
5986
- }
5987
- if (range.isBoxStart || range.isBoxEnd) {
5988
- this.moveBoxStripText();
5989
- this.history.save();
5990
- this.event.emit('input', inputEvent);
5991
- return;
5992
- }
5993
- if (inputEvent.inputType === 'insertText' ||
5994
- inputEvent.inputType === 'insertCompositionText') {
5995
- this.unsavedInputData += (_a = inputEvent.data) !== null && _a !== void 0 ? _a : '';
5996
- this.unsavedInputCount++;
5997
- if (this.unsavedInputData.length < this.config.minChangeSize) {
5998
- this.history.save({
5999
- inputType: 'insertText',
6000
- update: this.unsavedInputCount > 1,
6001
- });
6002
- }
6003
- else {
6004
- this.history.save({
6005
- inputType: 'insertText',
6006
- update: true,
6007
- });
6008
- this.resetUnsavedInputData();
6009
- }
6010
- this.event.emit('input', inputEvent);
6011
- return;
6012
- }
6013
- this.history.save();
6014
- this.event.emit('input', inputEvent);
6015
- }, 0);
6031
+ this.isComposing = inputEvent.isComposing;
6032
+ if (this.isComposing) {
6033
+ return;
6034
+ }
6035
+ this.handleInputEvent(event);
6016
6036
  });
6017
6037
  }
6018
6038
  // Binds events about history.
@@ -6043,8 +6063,8 @@ class Editor {
6043
6063
  this.history.event.on('save', (value, options) => {
6044
6064
  this.removeBoxGarbage();
6045
6065
  executeCommonMethods(value);
6046
- this.selection.sync();
6047
6066
  if (options.inputType !== 'insertText') {
6067
+ this.selection.sync();
6048
6068
  this.resetUnsavedInputData();
6049
6069
  }
6050
6070
  });
@@ -7246,16 +7266,16 @@ const codeBlockBox = {
7246
7266
  const boxValue = box.value;
7247
7267
  const langItem = langItemMap.get(boxValue.lang);
7248
7268
  const language = new Compartment();
7249
- const changeHandler = (value) => {
7250
- // Here setTimeout is necessary because isComposing is not false after ending composition.
7251
- window.setTimeout(() => {
7252
- if (editor.isComposing) {
7253
- return;
7254
- }
7255
- box.updateValue('code', value);
7256
- editor.history.save();
7257
- }, 0);
7258
- };
7269
+ const changeHandler = debounce((value) => {
7270
+ editor.selection.updateByRange();
7271
+ if (editor.isComposing) {
7272
+ return;
7273
+ }
7274
+ box.updateValue('code', value);
7275
+ editor.history.save();
7276
+ }, 1, {
7277
+ immediate: false,
7278
+ });
7259
7279
  const updateListener = EditorView.updateListener.of((update) => {
7260
7280
  if (!update.docChanged) {
7261
7281
  return;
@@ -7551,20 +7571,11 @@ function openFullScreen(box) {
7551
7571
  }
7552
7572
  return placeholderSrc;
7553
7573
  });
7554
- let savedRange;
7555
7574
  lightbox.on('openingAnimationEnd', () => {
7556
- savedRange = editor.selection.range;
7557
7575
  box.event.emit('openfullscreen');
7558
7576
  });
7559
7577
  lightbox.on('destroy', () => {
7560
- window.setTimeout(() => {
7561
- if (savedRange) {
7562
- // fix(image): lose focus when zooming in the iOS
7563
- editor.selection.range = savedRange;
7564
- editor.selection.sync();
7565
- }
7566
- box.event.emit('closefullscreen');
7567
- }, 0);
7578
+ box.event.emit('closefullscreen');
7568
7579
  });
7569
7580
  lightbox.init();
7570
7581
  lightbox.loadAndOpen(currentIndex);
@@ -9122,11 +9133,11 @@ var formatPainter = (editor) => {
9122
9133
  markList = [];
9123
9134
  editor.history.save();
9124
9135
  });
9125
- editor.event.on('click', (tagetNode) => {
9126
- if (tagetNode.isInside && tagetNode.closestContainer().get(0) === editor.container.get(0)) {
9136
+ editor.event.on('click', (targetNode) => {
9137
+ if (editor.container.contains(targetNode)) {
9127
9138
  return;
9128
9139
  }
9129
- const buttonNode = tagetNode.closest('button[name="formatPainter"]');
9140
+ const buttonNode = targetNode.closest('button[name="formatPainter"]');
9130
9141
  if (buttonNode.length > 0) {
9131
9142
  return;
9132
9143
  }
@@ -9420,9 +9431,7 @@ var link = (editor) => {
9420
9431
  popup.hide();
9421
9432
  return;
9422
9433
  }
9423
- if (linkNode.isOutside ||
9424
- linkNode.closestContainer().get(0) !== editor.container.get(0) ||
9425
- linkNode.closest('lake-box').length > 0) {
9434
+ if (!editor.container.contains(linkNode) || linkNode.closest('lake-box').length > 0) {
9426
9435
  popup.hide();
9427
9436
  return;
9428
9437
  }
@@ -10177,7 +10186,8 @@ var backspaceKey = (editor) => {
10177
10186
  const boxNode = range.commonAncestor.closest('lake-box');
10178
10187
  const box = getBox(boxNode);
10179
10188
  const boxValue = box.value;
10180
- if (box.name === 'codeBlock' && (boxValue.code === undefined || boxValue.code === '')) {
10189
+ if (range.isCollapsed && box.name === 'codeBlock' &&
10190
+ (boxValue.code === undefined || boxValue.code === '')) {
10181
10191
  event.preventDefault();
10182
10192
  editor.selection.removeBox(box);
10183
10193
  editor.history.save();