25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

69 satır
2.0 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. * @flow strict-local
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. import type ContentState from "./ContentState";
  13. import type { EntityMap } from "./EntityMap";
  14. import type SelectionState from "./SelectionState";
  15. const {
  16. notEmptyKey
  17. } = require("./draftKeyUtils");
  18. /**
  19. * Return the entity key that should be used when inserting text for the
  20. * specified target selection, only if the entity is `MUTABLE`. `IMMUTABLE`
  21. * and `SEGMENTED` entities should not be used for insertion behavior.
  22. */
  23. function getEntityKeyForSelection(contentState: ContentState, targetSelection: SelectionState): ?string {
  24. let entityKey;
  25. if (targetSelection.isCollapsed()) {
  26. const key = targetSelection.getAnchorKey();
  27. const offset = targetSelection.getAnchorOffset();
  28. if (offset > 0) {
  29. entityKey = contentState.getBlockForKey(key).getEntityAt(offset - 1);
  30. if (entityKey !== contentState.getBlockForKey(key).getEntityAt(offset)) {
  31. return null;
  32. }
  33. return filterKey(contentState.getEntityMap(), entityKey);
  34. }
  35. return null;
  36. }
  37. const startKey = targetSelection.getStartKey();
  38. const startOffset = targetSelection.getStartOffset();
  39. const startBlock = contentState.getBlockForKey(startKey);
  40. entityKey = startOffset === startBlock.getLength() ? null : startBlock.getEntityAt(startOffset);
  41. return filterKey(contentState.getEntityMap(), entityKey);
  42. }
  43. /**
  44. * Determine whether an entity key corresponds to a `MUTABLE` entity. If so,
  45. * return it. If not, return null.
  46. */
  47. function filterKey(entityMap: EntityMap, entityKey: ?string): ?string {
  48. if (notEmptyKey(entityKey)) {
  49. const entity = entityMap.__get(entityKey);
  50. return entity.getMutability() === 'MUTABLE' ? entityKey : null;
  51. }
  52. return null;
  53. }
  54. module.exports = getEntityKeyForSelection;