選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

127 行
3.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
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. import type { BlockNode, BlockNodeConfig, BlockNodeKey } from "./BlockNode";
  13. import type { DraftBlockType } from "./DraftBlockType";
  14. import type { DraftInlineStyle } from "./DraftInlineStyle";
  15. const CharacterMetadata = require("./CharacterMetadata");
  16. const findRangesImmutable = require("./findRangesImmutable");
  17. const Immutable = require("immutable");
  18. const {
  19. List,
  20. Map,
  21. OrderedSet,
  22. Record,
  23. Repeat
  24. } = Immutable;
  25. const EMPTY_SET = OrderedSet();
  26. const defaultRecord: BlockNodeConfig = {
  27. key: '',
  28. type: 'unstyled',
  29. text: '',
  30. characterList: List(),
  31. depth: 0,
  32. data: Map()
  33. };
  34. const ContentBlockRecord = (Record(defaultRecord): any);
  35. const decorateCharacterList = (config: BlockNodeConfig): BlockNodeConfig => {
  36. if (!config) {
  37. return config;
  38. }
  39. const {
  40. characterList,
  41. text
  42. } = config;
  43. if (text && !characterList) {
  44. config.characterList = List(Repeat(CharacterMetadata.EMPTY, text.length));
  45. }
  46. return config;
  47. };
  48. class ContentBlock extends ContentBlockRecord implements BlockNode {
  49. constructor(config: BlockNodeConfig) {
  50. super(decorateCharacterList(config));
  51. }
  52. getKey(): BlockNodeKey {
  53. return this.get('key');
  54. }
  55. getType(): DraftBlockType {
  56. return this.get('type');
  57. }
  58. getText(): string {
  59. return this.get('text');
  60. }
  61. getCharacterList(): List<CharacterMetadata> {
  62. return this.get('characterList');
  63. }
  64. getLength(): number {
  65. return this.getText().length;
  66. }
  67. getDepth(): number {
  68. return this.get('depth');
  69. }
  70. getData(): Map<any, any> {
  71. return this.get('data');
  72. }
  73. getInlineStyleAt(offset: number): DraftInlineStyle {
  74. const character = this.getCharacterList().get(offset);
  75. return character ? character.getStyle() : EMPTY_SET;
  76. }
  77. getEntityAt(offset: number): ?string {
  78. const character = this.getCharacterList().get(offset);
  79. return character ? character.getEntity() : null;
  80. }
  81. /**
  82. * Execute a callback for every contiguous range of styles within the block.
  83. */
  84. findStyleRanges(filterFn: (value: CharacterMetadata) => boolean, callback: (start: number, end: number) => void): void {
  85. findRangesImmutable(this.getCharacterList(), haveEqualStyle, filterFn, callback);
  86. }
  87. /**
  88. * Execute a callback for every contiguous range of entities within the block.
  89. */
  90. findEntityRanges(filterFn: (value: CharacterMetadata) => boolean, callback: (start: number, end: number) => void): void {
  91. findRangesImmutable(this.getCharacterList(), haveEqualEntity, filterFn, callback);
  92. }
  93. }
  94. function haveEqualStyle(charA: CharacterMetadata, charB: CharacterMetadata): boolean {
  95. return charA.getStyle() === charB.getStyle();
  96. }
  97. function haveEqualEntity(charA: CharacterMetadata, charB: CharacterMetadata): boolean {
  98. return charA.getEntity() === charB.getEntity();
  99. }
  100. module.exports = ContentBlock;