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

78 строки
2.3 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 { BlockNodeRecord } from "./BlockNodeRecord";
  13. import type CharacterMetadata from "./CharacterMetadata";
  14. import type { DraftBlockRenderMap } from "./DraftBlockRenderMap";
  15. import type { DraftBlockType } from "./DraftBlockType";
  16. import type { EntityMap } from "./EntityMap";
  17. const ContentBlock = require("./ContentBlock");
  18. const ContentBlockNode = require("./ContentBlockNode");
  19. const convertFromHTMLToContentBlocks = require("./convertFromHTMLToContentBlocks");
  20. const generateRandomKey = require("./generateRandomKey");
  21. const getSafeBodyFromHTML = require("./getSafeBodyFromHTML");
  22. const gkx = require("./gkx");
  23. const Immutable = require("immutable");
  24. const sanitizeDraftText = require("./sanitizeDraftText");
  25. const {
  26. List,
  27. Repeat
  28. } = Immutable;
  29. const experimentalTreeDataSupport = gkx('draft_tree_data_support');
  30. const ContentBlockRecord = experimentalTreeDataSupport ? ContentBlockNode : ContentBlock;
  31. const DraftPasteProcessor = {
  32. processHTML(html: string, blockRenderMap?: DraftBlockRenderMap): ?{
  33. contentBlocks: ?Array<BlockNodeRecord>,
  34. entityMap: EntityMap,
  35. ...
  36. } {
  37. return convertFromHTMLToContentBlocks(html, getSafeBodyFromHTML, blockRenderMap);
  38. },
  39. processText(textBlocks: Array<string>, character: CharacterMetadata, type: DraftBlockType): Array<BlockNodeRecord> {
  40. return textBlocks.reduce((acc, textLine, index) => {
  41. textLine = sanitizeDraftText(textLine);
  42. const key = generateRandomKey();
  43. let blockNodeConfig = {
  44. key,
  45. type,
  46. text: textLine,
  47. characterList: List(Repeat(character, textLine.length))
  48. }; // next block updates previous block
  49. if (experimentalTreeDataSupport && index !== 0) {
  50. const prevSiblingIndex = index - 1; // update previous block
  51. const previousBlock = acc[prevSiblingIndex] = acc[prevSiblingIndex].merge({
  52. nextSibling: key
  53. });
  54. blockNodeConfig = { ...blockNodeConfig,
  55. prevSibling: previousBlock.getKey()
  56. };
  57. }
  58. acc.push(new ContentBlockRecord(blockNodeConfig));
  59. return acc;
  60. }, []);
  61. }
  62. };
  63. module.exports = DraftPasteProcessor;