Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

160 lignes
5.2 KiB

  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. *
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  13. var UserAgent = require("fbjs/lib/UserAgent");
  14. var findAncestorOffsetKey = require("./findAncestorOffsetKey");
  15. var getWindowForNode = require("./getWindowForNode");
  16. var Immutable = require("immutable");
  17. var invariant = require("fbjs/lib/invariant");
  18. var nullthrows = require("fbjs/lib/nullthrows");
  19. var Map = Immutable.Map;
  20. // Heavily based on Prosemirror's DOMObserver https://github.com/ProseMirror/prosemirror-view/blob/master/src/domobserver.js
  21. var DOM_OBSERVER_OPTIONS = {
  22. subtree: true,
  23. characterData: true,
  24. childList: true,
  25. characterDataOldValue: false,
  26. attributes: false
  27. }; // IE11 has very broken mutation observers, so we also listen to DOMCharacterDataModified
  28. var USE_CHAR_DATA = UserAgent.isBrowser('IE <= 11');
  29. var DOMObserver =
  30. /*#__PURE__*/
  31. function () {
  32. function DOMObserver(container) {
  33. var _this = this;
  34. _defineProperty(this, "observer", void 0);
  35. _defineProperty(this, "container", void 0);
  36. _defineProperty(this, "mutations", void 0);
  37. _defineProperty(this, "onCharData", void 0);
  38. this.container = container;
  39. this.mutations = Map();
  40. var containerWindow = getWindowForNode(container);
  41. if (containerWindow.MutationObserver && !USE_CHAR_DATA) {
  42. this.observer = new containerWindow.MutationObserver(function (mutations) {
  43. return _this.registerMutations(mutations);
  44. });
  45. } else {
  46. this.onCharData = function (e) {
  47. !(e.target instanceof Node) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Expected target to be an instance of Node') : invariant(false) : void 0;
  48. _this.registerMutation({
  49. type: 'characterData',
  50. target: e.target
  51. });
  52. };
  53. }
  54. }
  55. var _proto = DOMObserver.prototype;
  56. _proto.start = function start() {
  57. if (this.observer) {
  58. this.observer.observe(this.container, DOM_OBSERVER_OPTIONS);
  59. } else {
  60. /* $FlowFixMe(>=0.68.0 site=www,mobile) This event type is not defined
  61. * by Flow's standard library */
  62. this.container.addEventListener('DOMCharacterDataModified', this.onCharData);
  63. }
  64. };
  65. _proto.stopAndFlushMutations = function stopAndFlushMutations() {
  66. var observer = this.observer;
  67. if (observer) {
  68. this.registerMutations(observer.takeRecords());
  69. observer.disconnect();
  70. } else {
  71. /* $FlowFixMe(>=0.68.0 site=www,mobile) This event type is not defined
  72. * by Flow's standard library */
  73. this.container.removeEventListener('DOMCharacterDataModified', this.onCharData);
  74. }
  75. var mutations = this.mutations;
  76. this.mutations = Map();
  77. return mutations;
  78. };
  79. _proto.registerMutations = function registerMutations(mutations) {
  80. for (var i = 0; i < mutations.length; i++) {
  81. this.registerMutation(mutations[i]);
  82. }
  83. };
  84. _proto.getMutationTextContent = function getMutationTextContent(mutation) {
  85. var type = mutation.type,
  86. target = mutation.target,
  87. removedNodes = mutation.removedNodes;
  88. if (type === 'characterData') {
  89. // When `textContent` is '', there is a race condition that makes
  90. // getting the offsetKey from the target not possible.
  91. // These events are also followed by a `childList`, which is the one
  92. // we are able to retrieve the offsetKey and apply the '' text.
  93. if (target.textContent !== '') {
  94. // IE 11 considers the enter keypress that concludes the composition
  95. // as an input char. This strips that newline character so the draft
  96. // state does not receive spurious newlines.
  97. if (USE_CHAR_DATA) {
  98. return target.textContent.replace('\n', '');
  99. }
  100. return target.textContent;
  101. }
  102. } else if (type === 'childList') {
  103. if (removedNodes && removedNodes.length) {
  104. // `characterData` events won't happen or are ignored when
  105. // removing the last character of a leaf node, what happens
  106. // instead is a `childList` event with a `removedNodes` array.
  107. // For this case the textContent should be '' and
  108. // `DraftModifier.replaceText` will make sure the content is
  109. // updated properly.
  110. return '';
  111. } else if (target.textContent !== '') {
  112. // Typing Chinese in an empty block in MS Edge results in a
  113. // `childList` event with non-empty textContent.
  114. // See https://github.com/facebook/draft-js/issues/2082
  115. return target.textContent;
  116. }
  117. }
  118. return null;
  119. };
  120. _proto.registerMutation = function registerMutation(mutation) {
  121. var textContent = this.getMutationTextContent(mutation);
  122. if (textContent != null) {
  123. var offsetKey = nullthrows(findAncestorOffsetKey(mutation.target));
  124. this.mutations = this.mutations.set(offsetKey, textContent);
  125. }
  126. };
  127. return DOMObserver;
  128. }();
  129. module.exports = DOMObserver;