Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

getEntityKeyForSelection.js 1.7 KiB

před 3 roky
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. var _require = require("./draftKeyUtils"),
  13. notEmptyKey = _require.notEmptyKey;
  14. /**
  15. * Return the entity key that should be used when inserting text for the
  16. * specified target selection, only if the entity is `MUTABLE`. `IMMUTABLE`
  17. * and `SEGMENTED` entities should not be used for insertion behavior.
  18. */
  19. function getEntityKeyForSelection(contentState, targetSelection) {
  20. var entityKey;
  21. if (targetSelection.isCollapsed()) {
  22. var key = targetSelection.getAnchorKey();
  23. var offset = targetSelection.getAnchorOffset();
  24. if (offset > 0) {
  25. entityKey = contentState.getBlockForKey(key).getEntityAt(offset - 1);
  26. if (entityKey !== contentState.getBlockForKey(key).getEntityAt(offset)) {
  27. return null;
  28. }
  29. return filterKey(contentState.getEntityMap(), entityKey);
  30. }
  31. return null;
  32. }
  33. var startKey = targetSelection.getStartKey();
  34. var startOffset = targetSelection.getStartOffset();
  35. var startBlock = contentState.getBlockForKey(startKey);
  36. entityKey = startOffset === startBlock.getLength() ? null : startBlock.getEntityAt(startOffset);
  37. return filterKey(contentState.getEntityMap(), entityKey);
  38. }
  39. /**
  40. * Determine whether an entity key corresponds to a `MUTABLE` entity. If so,
  41. * return it. If not, return null.
  42. */
  43. function filterKey(entityMap, entityKey) {
  44. if (notEmptyKey(entityKey)) {
  45. var entity = entityMap.__get(entityKey);
  46. return entity.getMutability() === 'MUTABLE' ? entityKey : null;
  47. }
  48. return null;
  49. }
  50. module.exports = getEntityKeyForSelection;