You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

68 lines
2.6 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. *
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
  13. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  14. var ContentBlock = require("./ContentBlock");
  15. var ContentBlockNode = require("./ContentBlockNode");
  16. var convertFromHTMLToContentBlocks = require("./convertFromHTMLToContentBlocks");
  17. var generateRandomKey = require("./generateRandomKey");
  18. var getSafeBodyFromHTML = require("./getSafeBodyFromHTML");
  19. var gkx = require("./gkx");
  20. var Immutable = require("immutable");
  21. var sanitizeDraftText = require("./sanitizeDraftText");
  22. var List = Immutable.List,
  23. Repeat = Immutable.Repeat;
  24. var experimentalTreeDataSupport = gkx('draft_tree_data_support');
  25. var ContentBlockRecord = experimentalTreeDataSupport ? ContentBlockNode : ContentBlock;
  26. var DraftPasteProcessor = {
  27. processHTML: function processHTML(html, blockRenderMap) {
  28. return convertFromHTMLToContentBlocks(html, getSafeBodyFromHTML, blockRenderMap);
  29. },
  30. processText: function processText(textBlocks, character, type) {
  31. return textBlocks.reduce(function (acc, textLine, index) {
  32. textLine = sanitizeDraftText(textLine);
  33. var key = generateRandomKey();
  34. var blockNodeConfig = {
  35. key: key,
  36. type: type,
  37. text: textLine,
  38. characterList: List(Repeat(character, textLine.length))
  39. }; // next block updates previous block
  40. if (experimentalTreeDataSupport && index !== 0) {
  41. var prevSiblingIndex = index - 1; // update previous block
  42. var previousBlock = acc[prevSiblingIndex] = acc[prevSiblingIndex].merge({
  43. nextSibling: key
  44. });
  45. blockNodeConfig = _objectSpread({}, blockNodeConfig, {
  46. prevSibling: previousBlock.getKey()
  47. });
  48. }
  49. acc.push(new ContentBlockRecord(blockNodeConfig));
  50. return acc;
  51. }, []);
  52. }
  53. };
  54. module.exports = DraftPasteProcessor;