Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

283 рядки
10 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. var DraftModifier = require("./DraftModifier");
  13. var EditorState = require("./EditorState");
  14. var adjustBlockDepthForContentState = require("./adjustBlockDepthForContentState");
  15. var nullthrows = require("fbjs/lib/nullthrows");
  16. var RichTextEditorUtil = {
  17. currentBlockContainsLink: function currentBlockContainsLink(editorState) {
  18. var selection = editorState.getSelection();
  19. var contentState = editorState.getCurrentContent();
  20. var entityMap = contentState.getEntityMap();
  21. return contentState.getBlockForKey(selection.getAnchorKey()).getCharacterList().slice(selection.getStartOffset(), selection.getEndOffset()).some(function (v) {
  22. var entity = v.getEntity();
  23. return !!entity && entityMap.__get(entity).getType() === 'LINK';
  24. });
  25. },
  26. getCurrentBlockType: function getCurrentBlockType(editorState) {
  27. var selection = editorState.getSelection();
  28. return editorState.getCurrentContent().getBlockForKey(selection.getStartKey()).getType();
  29. },
  30. getDataObjectForLinkURL: function getDataObjectForLinkURL(uri) {
  31. return {
  32. url: uri.toString()
  33. };
  34. },
  35. handleKeyCommand: function handleKeyCommand(editorState, command, eventTimeStamp) {
  36. switch (command) {
  37. case 'bold':
  38. return RichTextEditorUtil.toggleInlineStyle(editorState, 'BOLD');
  39. case 'italic':
  40. return RichTextEditorUtil.toggleInlineStyle(editorState, 'ITALIC');
  41. case 'underline':
  42. return RichTextEditorUtil.toggleInlineStyle(editorState, 'UNDERLINE');
  43. case 'code':
  44. return RichTextEditorUtil.toggleCode(editorState);
  45. case 'backspace':
  46. case 'backspace-word':
  47. case 'backspace-to-start-of-line':
  48. return RichTextEditorUtil.onBackspace(editorState);
  49. case 'delete':
  50. case 'delete-word':
  51. case 'delete-to-end-of-block':
  52. return RichTextEditorUtil.onDelete(editorState);
  53. default:
  54. // they may have custom editor commands; ignore those
  55. return null;
  56. }
  57. },
  58. insertSoftNewline: function insertSoftNewline(editorState) {
  59. var contentState = DraftModifier.insertText(editorState.getCurrentContent(), editorState.getSelection(), '\n', editorState.getCurrentInlineStyle(), null);
  60. var newEditorState = EditorState.push(editorState, contentState, 'insert-characters');
  61. return EditorState.forceSelection(newEditorState, contentState.getSelectionAfter());
  62. },
  63. /**
  64. * For collapsed selections at the start of styled blocks, backspace should
  65. * just remove the existing style.
  66. */
  67. onBackspace: function onBackspace(editorState) {
  68. var selection = editorState.getSelection();
  69. if (!selection.isCollapsed() || selection.getAnchorOffset() || selection.getFocusOffset()) {
  70. return null;
  71. } // First, try to remove a preceding atomic block.
  72. var content = editorState.getCurrentContent();
  73. var startKey = selection.getStartKey();
  74. var blockBefore = content.getBlockBefore(startKey);
  75. if (blockBefore && blockBefore.getType() === 'atomic') {
  76. var blockMap = content.getBlockMap()["delete"](blockBefore.getKey());
  77. var withoutAtomicBlock = content.merge({
  78. blockMap: blockMap,
  79. selectionAfter: selection
  80. });
  81. if (withoutAtomicBlock !== content) {
  82. return EditorState.push(editorState, withoutAtomicBlock, 'remove-range');
  83. }
  84. } // If that doesn't succeed, try to remove the current block style.
  85. var withoutBlockStyle = RichTextEditorUtil.tryToRemoveBlockStyle(editorState);
  86. if (withoutBlockStyle) {
  87. return EditorState.push(editorState, withoutBlockStyle, 'change-block-type');
  88. }
  89. return null;
  90. },
  91. onDelete: function onDelete(editorState) {
  92. var selection = editorState.getSelection();
  93. if (!selection.isCollapsed()) {
  94. return null;
  95. }
  96. var content = editorState.getCurrentContent();
  97. var startKey = selection.getStartKey();
  98. var block = content.getBlockForKey(startKey);
  99. var length = block.getLength(); // The cursor is somewhere within the text. Behave normally.
  100. if (selection.getStartOffset() < length) {
  101. return null;
  102. }
  103. var blockAfter = content.getBlockAfter(startKey);
  104. if (!blockAfter || blockAfter.getType() !== 'atomic') {
  105. return null;
  106. }
  107. var atomicBlockTarget = selection.merge({
  108. focusKey: blockAfter.getKey(),
  109. focusOffset: blockAfter.getLength()
  110. });
  111. var withoutAtomicBlock = DraftModifier.removeRange(content, atomicBlockTarget, 'forward');
  112. if (withoutAtomicBlock !== content) {
  113. return EditorState.push(editorState, withoutAtomicBlock, 'remove-range');
  114. }
  115. return null;
  116. },
  117. onTab: function onTab(event, editorState, maxDepth) {
  118. var selection = editorState.getSelection();
  119. var key = selection.getAnchorKey();
  120. if (key !== selection.getFocusKey()) {
  121. return editorState;
  122. }
  123. var content = editorState.getCurrentContent();
  124. var block = content.getBlockForKey(key);
  125. var type = block.getType();
  126. if (type !== 'unordered-list-item' && type !== 'ordered-list-item') {
  127. return editorState;
  128. }
  129. event.preventDefault();
  130. var depth = block.getDepth();
  131. if (!event.shiftKey && depth === maxDepth) {
  132. return editorState;
  133. }
  134. var withAdjustment = adjustBlockDepthForContentState(content, selection, event.shiftKey ? -1 : 1, maxDepth);
  135. return EditorState.push(editorState, withAdjustment, 'adjust-depth');
  136. },
  137. toggleBlockType: function toggleBlockType(editorState, blockType) {
  138. var selection = editorState.getSelection();
  139. var startKey = selection.getStartKey();
  140. var endKey = selection.getEndKey();
  141. var content = editorState.getCurrentContent();
  142. var target = selection; // Triple-click can lead to a selection that includes offset 0 of the
  143. // following block. The `SelectionState` for this case is accurate, but
  144. // we should avoid toggling block type for the trailing block because it
  145. // is a confusing interaction.
  146. if (startKey !== endKey && selection.getEndOffset() === 0) {
  147. var blockBefore = nullthrows(content.getBlockBefore(endKey));
  148. endKey = blockBefore.getKey();
  149. target = target.merge({
  150. anchorKey: startKey,
  151. anchorOffset: selection.getStartOffset(),
  152. focusKey: endKey,
  153. focusOffset: blockBefore.getLength(),
  154. isBackward: false
  155. });
  156. }
  157. var hasAtomicBlock = content.getBlockMap().skipWhile(function (_, k) {
  158. return k !== startKey;
  159. }).reverse().skipWhile(function (_, k) {
  160. return k !== endKey;
  161. }).some(function (v) {
  162. return v.getType() === 'atomic';
  163. });
  164. if (hasAtomicBlock) {
  165. return editorState;
  166. }
  167. var typeToSet = content.getBlockForKey(startKey).getType() === blockType ? 'unstyled' : blockType;
  168. return EditorState.push(editorState, DraftModifier.setBlockType(content, target, typeToSet), 'change-block-type');
  169. },
  170. toggleCode: function toggleCode(editorState) {
  171. var selection = editorState.getSelection();
  172. var anchorKey = selection.getAnchorKey();
  173. var focusKey = selection.getFocusKey();
  174. if (selection.isCollapsed() || anchorKey !== focusKey) {
  175. return RichTextEditorUtil.toggleBlockType(editorState, 'code-block');
  176. }
  177. return RichTextEditorUtil.toggleInlineStyle(editorState, 'CODE');
  178. },
  179. /**
  180. * Toggle the specified inline style for the selection. If the
  181. * user's selection is collapsed, apply or remove the style for the
  182. * internal state. If it is not collapsed, apply the change directly
  183. * to the document state.
  184. */
  185. toggleInlineStyle: function toggleInlineStyle(editorState, inlineStyle) {
  186. var selection = editorState.getSelection();
  187. var currentStyle = editorState.getCurrentInlineStyle(); // If the selection is collapsed, toggle the specified style on or off and
  188. // set the result as the new inline style override. This will then be
  189. // used as the inline style for the next character to be inserted.
  190. if (selection.isCollapsed()) {
  191. return EditorState.setInlineStyleOverride(editorState, currentStyle.has(inlineStyle) ? currentStyle.remove(inlineStyle) : currentStyle.add(inlineStyle));
  192. } // If characters are selected, immediately apply or remove the
  193. // inline style on the document state itself.
  194. var content = editorState.getCurrentContent();
  195. var newContent; // If the style is already present for the selection range, remove it.
  196. // Otherwise, apply it.
  197. if (currentStyle.has(inlineStyle)) {
  198. newContent = DraftModifier.removeInlineStyle(content, selection, inlineStyle);
  199. } else {
  200. newContent = DraftModifier.applyInlineStyle(content, selection, inlineStyle);
  201. }
  202. return EditorState.push(editorState, newContent, 'change-inline-style');
  203. },
  204. toggleLink: function toggleLink(editorState, targetSelection, entityKey) {
  205. var withoutLink = DraftModifier.applyEntity(editorState.getCurrentContent(), targetSelection, entityKey);
  206. return EditorState.push(editorState, withoutLink, 'apply-entity');
  207. },
  208. /**
  209. * When a collapsed cursor is at the start of a styled block, changes block
  210. * type to 'unstyled'. Returns null if selection does not meet that criteria.
  211. */
  212. tryToRemoveBlockStyle: function tryToRemoveBlockStyle(editorState) {
  213. var selection = editorState.getSelection();
  214. var offset = selection.getAnchorOffset();
  215. if (selection.isCollapsed() && offset === 0) {
  216. var key = selection.getAnchorKey();
  217. var content = editorState.getCurrentContent();
  218. var block = content.getBlockForKey(key);
  219. var type = block.getType();
  220. var blockBefore = content.getBlockBefore(key);
  221. if (type === 'code-block' && blockBefore && blockBefore.getType() === 'code-block' && blockBefore.getLength() !== 0) {
  222. return null;
  223. }
  224. if (type !== 'unstyled') {
  225. return DraftModifier.setBlockType(content, selection, 'unstyled');
  226. }
  227. }
  228. return null;
  229. }
  230. };
  231. module.exports = RichTextEditorUtil;