Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

98 строки
2.8 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
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. import type { DraftInlineStyle } from "./DraftInlineStyle";
  13. const {
  14. Map,
  15. OrderedSet,
  16. Record
  17. } = require("immutable"); // Immutable.map is typed such that the value for every key in the map
  18. // must be the same type
  19. type CharacterMetadataConfigValueType = DraftInlineStyle | ?string;
  20. type CharacterMetadataConfig = {
  21. style?: CharacterMetadataConfigValueType,
  22. entity?: CharacterMetadataConfigValueType,
  23. ...
  24. };
  25. const EMPTY_SET = OrderedSet();
  26. const defaultRecord: CharacterMetadataConfig = {
  27. style: EMPTY_SET,
  28. entity: null
  29. };
  30. const CharacterMetadataRecord = (Record(defaultRecord): any);
  31. class CharacterMetadata extends CharacterMetadataRecord {
  32. getStyle(): DraftInlineStyle {
  33. return this.get('style');
  34. }
  35. getEntity(): ?string {
  36. return this.get('entity');
  37. }
  38. hasStyle(style: string): boolean {
  39. return this.getStyle().includes(style);
  40. }
  41. static applyStyle(record: CharacterMetadata, style: string): CharacterMetadata {
  42. const withStyle = record.set('style', record.getStyle().add(style));
  43. return CharacterMetadata.create(withStyle);
  44. }
  45. static removeStyle(record: CharacterMetadata, style: string): CharacterMetadata {
  46. const withoutStyle = record.set('style', record.getStyle().remove(style));
  47. return CharacterMetadata.create(withoutStyle);
  48. }
  49. static applyEntity(record: CharacterMetadata, entityKey: ?string): CharacterMetadata {
  50. const withEntity = record.getEntity() === entityKey ? record : record.set('entity', entityKey);
  51. return CharacterMetadata.create(withEntity);
  52. }
  53. /**
  54. * Use this function instead of the `CharacterMetadata` constructor.
  55. * Since most content generally uses only a very small number of
  56. * style/entity permutations, we can reuse these objects as often as
  57. * possible.
  58. */
  59. static create(config?: CharacterMetadataConfig): CharacterMetadata {
  60. if (!config) {
  61. return EMPTY;
  62. }
  63. const defaultConfig: CharacterMetadataConfig = {
  64. style: EMPTY_SET,
  65. entity: (null: ?string)
  66. }; // Fill in unspecified properties, if necessary.
  67. const configMap = Map(defaultConfig).merge(config);
  68. const existing: ?CharacterMetadata = pool.get(configMap);
  69. if (existing) {
  70. return existing;
  71. }
  72. const newCharacter = new CharacterMetadata(configMap);
  73. pool = pool.set(configMap, newCharacter);
  74. return newCharacter;
  75. }
  76. }
  77. const EMPTY = new CharacterMetadata();
  78. let pool: Map<Map<any, any>, CharacterMetadata> = Map([[Map(defaultRecord), EMPTY]]);
  79. CharacterMetadata.EMPTY = EMPTY;
  80. module.exports = CharacterMetadata;