UNPKG

10.3 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.noop = noop;
7exports.getApplyFontStyleFunc = getApplyFontStyleFunc;
8exports.getToggleFontStyleFunc = getToggleFontStyleFunc;
9exports.findEntities = findEntities;
10exports.getSelectionText = getSelectionText;
11exports.getApplyEntityFunc = getApplyEntityFunc;
12exports.getToggleEntityFunc = getToggleEntityFunc;
13exports.getCurrentEntity = getCurrentEntity;
14exports.inlineStyleComponentFactory = inlineStyleComponentFactory;
15exports.blockStyleComponentFactory = blockStyleComponentFactory;
16
17var _react = require('react');
18
19var React = _interopRequireWildcard(_react);
20
21var _draftJs = require('draft-js');
22
23var _classnames3 = require('classnames');
24
25var _classnames4 = _interopRequireDefault(_classnames3);
26
27var _rcEditorUtils = require('rc-editor-utils');
28
29function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
30
31function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj["default"] = obj; return newObj; } }
32
33function _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; }
34
35var __assign = undefined && undefined.__assign || Object.assign || function (t) {
36 for (var s, i = 1, n = arguments.length; i < n; i++) {
37 s = arguments[i];
38 for (var p in s) {
39 if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
40 }
41 }
42 return t;
43};
44function noop(args) {}
45function getApplyFontStyleFunc(prefix, callbacks) {
46 return function applyStyle(styleName) {
47 var needFocus = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
48 var getEditorState = callbacks.getEditorState,
49 setEditorState = callbacks.setEditorState;
50
51 var editorState = getEditorState();
52 var contentState = editorState.getCurrentContent();
53 var selection = editorState.getSelection();
54 var currentStyle = (0, _rcEditorUtils.getCurrentInlineStyle)(editorState);
55 if (selection.isCollapsed()) {
56 currentStyle.forEach(function (style) {
57 if (style.indexOf('' + prefix) !== -1 && style !== styleName) {
58 editorState = _draftJs.RichUtils.toggleInlineStyle(editorState, style);
59 }
60 });
61 editorState = _draftJs.RichUtils.toggleInlineStyle(editorState, styleName);
62 return setEditorState(editorState, true);
63 }
64 currentStyle.forEach(function (style) {
65 if (style.indexOf('' + prefix) !== -1) {
66 contentState = _draftJs.Modifier.removeInlineStyle(contentState, selection, style);
67 }
68 });
69 contentState = _draftJs.Modifier.applyInlineStyle(contentState, selection, styleName);
70 setEditorState(_draftJs.EditorState.push(editorState, contentState, 'apply-style'), needFocus);
71 };
72}
73function getToggleFontStyleFunc(prefix, callbacks) {
74 return function toggleStyle(styleName) {
75 var getEditorState = callbacks.getEditorState,
76 setEditorState = callbacks.setEditorState;
77
78 var editorState = getEditorState(true);
79 var selection = editorState.getSelection();
80 var currentStyle = (0, _rcEditorUtils.getCurrentInlineStyle)(editorState);
81 currentStyle.forEach(function (style) {
82 if (style.indexOf('' + prefix) !== -1 && style !== styleName) {
83 editorState = _draftJs.RichUtils.toggleInlineStyle(editorState, style);
84 }
85 });
86 editorState = _draftJs.RichUtils.toggleInlineStyle(editorState, styleName);
87 if (selection.isCollapsed()) {
88 return setEditorState(editorState, true);
89 }
90 setEditorState(editorState);
91 };
92}
93function findEntities(entityType) {
94 return function findEntitiesFunc(contentBlock, callback, contentState) {
95 contentBlock.findEntityRanges(function (character) {
96 var entityKey = character.getEntity();
97 return entityKey !== null && contentState.getEntity(entityKey).getType() === entityType;
98 }, callback);
99 };
100}
101function getSelectionText(editorState, selection) {
102 var anchorKey = selection.getAnchorKey();
103 var currentContent = editorState.getCurrentContent();
104 var currentBlock = currentContent.getBlockForKey(anchorKey);
105 return currentBlock.getText();
106}
107function getApplyEntityFunc(callbacks) {
108 return function applyEntity(entityType) {
109 var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
110 var entityMode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'MUTABLE';
111 var getEditorState = callbacks.getEditorState,
112 setEditorState = callbacks.setEditorState;
113
114 var editorState = getEditorState();
115 var contentState = editorState.getCurrentContent();
116 var selection = editorState.getSelection();
117 var currentEntity = getCurrentEntity(editorState);
118 var entityKey = contentState.createEntity(entityType, entityMode, data);
119 var replacedContent = _draftJs.Modifier.applyEntity(contentState, selection, entityKey);
120 return setEditorState(_draftJs.EditorState.push(editorState, replacedContent, 'toggle-block'));
121 };
122}
123function getToggleEntityFunc(callbacks) {
124 return function toggleEntity(entityType) {
125 var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
126 var entityMode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'MUTABLE';
127 var getEditorState = callbacks.getEditorState,
128 setEditorState = callbacks.setEditorState;
129
130 var editorState = getEditorState();
131 var contentState = editorState.getCurrentContent();
132 var selection = editorState.getSelection();
133 var entityKey = null;
134 var currentEntity = getCurrentEntity(editorState);
135 if (!currentEntity || contentState.getEntity(currentEntity).getType() !== entityType) {
136 contentState.createEntity(entityType, entityMode, data);
137 entityKey = contentState.getLastCreatedEntityKey();
138 }
139 var replacedContent = _draftJs.Modifier.applyEntity(editorState.getCurrentContent(), selection, entityKey);
140 return setEditorState(_draftJs.EditorState.push(editorState, replacedContent, 'toggle-block'));
141 };
142}
143function getCurrentEntity(editorState) {
144 var entity = void 0;
145 var selection = editorState.getSelection();
146 var start = selection.getStartOffset();
147 var end = selection.getEndOffset();
148 if (start === end && start === 0) {
149 end = -1;
150 } else if (start === end) {
151 start = start - 1;
152 }
153 var block = (0, _rcEditorUtils.getSelectedBlock)(editorState);
154 for (var i = start; i < end; i++) {
155 var currentEntity = block.getEntityAt(i);
156 if (!currentEntity) {
157 entity = undefined;
158 break;
159 }
160 if (i === start) {
161 entity = currentEntity;
162 } else {
163 if (entity !== currentEntity) {
164 entity = undefined;
165 break;
166 }
167 }
168 }
169 return entity;
170}
171function inlineStyleComponentFactory(name) {
172 var style = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
173
174 return {
175 constructor: function constructor() {
176 var callbacks = {
177 getEditorState: noop,
178 setEditorState: noop
179 };
180 var upperName = name.toUpperCase();
181 var toggleStyle = (0, _rcEditorUtils.getToggleStyleFunc)(callbacks);
182 return {
183 name: name,
184 callbacks: callbacks,
185 component: function component(props) {
186 var _classnames;
187
188 var currentStyle = (0, _rcEditorUtils.getCurrentInlineStyle)(callbacks.getEditorState());
189 var classNames = (0, _classnames4["default"])((_classnames = {}, _defineProperty(_classnames, 'editor-icon', true), _defineProperty(_classnames, 'editor-icon-' + name, true), _defineProperty(_classnames, 'active', currentStyle.has(upperName)), _classnames));
190 return React.createElement("span", { onMouseDown: function onMouseDown(e) {
191 toggleStyle(upperName);e.preventDefault();
192 }, className: classNames });
193 }
194 };
195 },
196
197 config: {}
198 };
199}
200function blockStyleComponentFactory(name, style) {
201 return {
202 constructor: function constructor() {
203 var callbacks = {
204 getEditorState: noop,
205 setEditorState: noop
206 };
207 var blockRenderMap = _defineProperty({}, '' + name, {
208 element: function element(props) {
209 return React.createElement("div", __assign({}, props, { style: style }));
210 },
211 style: style
212 });
213 var toggleBlockStyle = (0, _rcEditorUtils.getToggleBlockStyleFunc)(callbacks);
214 return {
215 name: name,
216 callbacks: callbacks,
217 blockRenderMap: blockRenderMap,
218 component: function component(prop) {
219 var _classnames2;
220
221 var selectedBlock = (0, _rcEditorUtils.getSelectedBlock)(callbacks.getEditorState());
222 var classNames = (0, _classnames4["default"])((_classnames2 = {}, _defineProperty(_classnames2, 'editor-icon', true), _defineProperty(_classnames2, 'editor-icon-' + name, true), _defineProperty(_classnames2, 'active', selectedBlock.getType() === name), _classnames2));
223 return React.createElement("span", { onMouseDown: function onMouseDown() {
224 return toggleBlockStyle(name);
225 }, className: classNames });
226 }
227 };
228 },
229
230 config: {}
231 };
232}
\No newline at end of file