This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 244
/
index.js
185 lines (168 loc) · 5.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { Map, List } from 'immutable';
import { EditorState, ContentBlock, genKey } from 'draft-js';
import { Block, Entity } from '../util/constants';
/*
Returns default block-level metadata for various block type. Empty object otherwise.
*/
export const getDefaultBlockData = (blockType, initialData = {}) => {
switch (blockType) {
case Block.TODO: return { checked: false };
default: return initialData;
}
};
/*
Get currentBlock in the editorState.
*/
export const getCurrentBlock = (editorState) => {
const selectionState = editorState.getSelection();
const contentState = editorState.getCurrentContent();
const block = contentState.getBlockForKey(selectionState.getStartKey());
return block;
};
/*
Adds a new block (currently replaces an empty block) at the current cursor position
of the given `newType`.
*/
export const addNewBlock = (editorState, newType = Block.UNSTYLED, initialData = {}) => {
const selectionState = editorState.getSelection();
if (!selectionState.isCollapsed()) {
return editorState;
}
const contentState = editorState.getCurrentContent();
const key = selectionState.getStartKey();
const blockMap = contentState.getBlockMap();
const currentBlock = getCurrentBlock(editorState);
if (!currentBlock) {
return editorState;
}
if (currentBlock.getLength() === 0) {
if (currentBlock.getType() === newType) {
return editorState;
}
const newBlock = currentBlock.merge({
type: newType,
data: getDefaultBlockData(newType, initialData),
});
const newContentState = contentState.merge({
blockMap: blockMap.set(key, newBlock),
selectionAfter: selectionState,
});
return EditorState.push(editorState, newContentState, 'change-block-type');
}
return editorState;
};
/*
Changes the block type of the current block.
*/
export const resetBlockWithType = (editorState, newType = Block.UNSTYLED, overrides = {}) => {
const contentState = editorState.getCurrentContent();
const selectionState = editorState.getSelection();
const key = selectionState.getStartKey();
const blockMap = contentState.getBlockMap();
const block = blockMap.get(key);
const newBlock = block.mergeDeep(overrides, {
type: newType,
data: getDefaultBlockData(newType),
});
const newContentState = contentState.merge({
blockMap: blockMap.set(key, newBlock),
selectionAfter: selectionState.merge({
anchorOffset: 0,
focusOffset: 0,
}),
});
return EditorState.push(editorState, newContentState, 'change-block-type');
};
/*
Update block-level metadata of the given `block` to the `newData`/
*/
export const updateDataOfBlock = (editorState, block, newData) => {
const contentState = editorState.getCurrentContent();
const newBlock = block.merge({
data: newData,
});
const newContentState = contentState.merge({
blockMap: contentState.getBlockMap().set(block.getKey(), newBlock),
});
return EditorState.push(editorState, newContentState, 'change-block-data');
};
// const BEFORE = -1;
// const AFTER = 1;
/*
Used from [react-rte](https://github.com/sstur/react-rte/blob/master/src/lib/insertBlockAfter.js)
by [sstur](https://github.com/sstur)
*/
export const addNewBlockAt = (
editorState,
pivotBlockKey,
newBlockType = Block.UNSTYLED,
initialData = {}
) => {
const content = editorState.getCurrentContent();
const blockMap = content.getBlockMap();
const block = blockMap.get(pivotBlockKey);
if (!block) {
throw new Error(`The pivot key - ${pivotBlockKey} is not present in blockMap.`);
}
const blocksBefore = blockMap.toSeq().takeUntil((v) => (v === block));
const blocksAfter = blockMap.toSeq().skipUntil((v) => (v === block)).rest();
const newBlockKey = genKey();
const newBlock = new ContentBlock({
key: newBlockKey,
type: newBlockType,
text: '',
characterList: List(),
depth: 0,
data: Map(getDefaultBlockData(newBlockType, initialData)),
});
const newBlockMap = blocksBefore.concat(
[[pivotBlockKey, block], [newBlockKey, newBlock]],
blocksAfter
).toOrderedMap();
const selection = editorState.getSelection();
const newContent = content.merge({
blockMap: newBlockMap,
selectionBefore: selection,
selectionAfter: selection.merge({
anchorKey: newBlockKey,
anchorOffset: 0,
focusKey: newBlockKey,
focusOffset: 0,
isBackward: false,
}),
});
return EditorState.push(editorState, newContent, 'split-block');
};
/**
* Check whether the cursor is between entity of type LINK
*/
export const isCursorBetweenLink = (editorState) => {
let ret = null;
const selection = editorState.getSelection();
const content = editorState.getCurrentContent();
const currentBlock = getCurrentBlock(editorState);
if (!currentBlock) {
return ret;
}
let entityKey = null;
let blockKey = null;
if (currentBlock.getType() !== Block.ATOMIC && selection.isCollapsed()) {
if (currentBlock.getLength() > 0) {
if (selection.getAnchorOffset() > 0) {
entityKey = currentBlock.getEntityAt(selection.getAnchorOffset() - 1);
blockKey = currentBlock.getKey();
if (entityKey !== null) {
const entity = content.getEntity(entityKey);
if (entity.getType() === Entity.LINK) {
ret = {
entityKey,
blockKey,
url: entity.getData().url,
};
}
}
}
}
}
return ret;
};