Skip to content
This repository has been archived by the owner on Feb 21, 2019. It is now read-only.

Support latest version of Slate #83

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions lib/changes/decreaseItemDepth.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,28 @@ function decreaseItemDepth(opts: Options, change: Change): Change {
data: currentList.data
});
// Add the sublist
change.insertNodeByKey(
currentItem.key,
currentItem.nodes.size,
sublist,
{ normalize: false }
);

change.moveNodeByKey(
currentItem.key,
parentList.key,
parentList.nodes.indexOf(parentItem) + 1,
{ normalize: false }
);
change.withoutNormalizing(() => {
change.insertNodeByKey(
currentItem.key,
currentItem.nodes.size,
sublist
);

// Move the followingItems to the sublist
followingItems.forEach((item, index) =>
change.moveNodeByKey(
item.key,
sublist.key,
sublist.nodes.size + index,
{ normalize: false }
)
);
currentItem.key,
parentList.key,
parentList.nodes.indexOf(parentItem) + 1
);

// Move the followingItems to the sublist
followingItems.forEach((item, index) =>
change.moveNodeByKey(
item.key,
sublist.key,
sublist.nodes.size + index
)
);
});
} else {
change.moveNodeByKey(
currentItem.key,
Expand Down
7 changes: 3 additions & 4 deletions lib/changes/increaseItemDepth.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,10 @@ function moveAsSubItem(
data: currentList.data
});

change.insertNodeByKey(destKey, lastIndex, newSublist, {
normalize: false
return change.withoutNormalizing(() => {
change.insertNodeByKey(destKey, lastIndex, newSublist);
change.moveNodeByKey(item.key, newSublist.key, 0);
});

return change.moveNodeByKey(item.key, newSublist.key, 0);
}

export default increaseItemDepth;
5 changes: 3 additions & 2 deletions lib/changes/splitListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ function splitListItem(opts: Options, change: Change): Change {
return change;
}

const splitOffset = value.startOffset;
const { start } = value.selection;
const splitOffset = start.offset;

return change.splitDescendantsByKey(
currentItem.key,
value.startKey,
start.key,
splitOffset
);
}
Expand Down
46 changes: 21 additions & 25 deletions lib/changes/unwrapList.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,29 @@ function unwrapList(opts: Options, change: Change): Change {
return change;
}

// Unwrap the items from their list
items.forEach(item =>
change.unwrapNodeByKey(item.key, { normalize: false })
);

// Parent of the list of the items
const firstItem = items.first();
const parent = change.value.document.getParent(firstItem.key);

let index = parent.nodes.findIndex(node => node.key === firstItem.key);

// Unwrap the items' children
items.forEach(item => {
item.nodes.forEach(node => {
change.moveNodeByKey(node.key, parent.key, index, {
normalize: false
});
index += 1;
});
});
return change.withoutNormalizing(() => {
// Unwrap the items from their list
items.forEach(item => change.unwrapNodeByKey(item.key));

// Parent of the list of the items
const firstItem = items.first();
const parent = change.value.document.getParent(firstItem.key);

let index = parent.nodes.findIndex(node => node.key === firstItem.key);

// Finally, remove the now empty items
items.forEach(item =>
change.removeNodeByKey(item.key, { normalize: false })
);
// Unwrap the items' children
items.forEach(item =>
item.nodes.forEach(node => {
change.moveNodeByKey(node.key, parent.key, index);
index += 1;
})
);

return change;
// Finally, remove the now empty items
items.forEach(item => change.removeNodeByKey(item.key));

return change;
});
}

export default unwrapList;
39 changes: 17 additions & 22 deletions lib/changes/wrapInList.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,22 @@ function wrapInList(
const selectedBlocks = getHighestSelectedBlocks(change.value);
type = type || opts.types[0];

// Wrap in container
change.wrapBlock(
{
change.withoutNormalizing(() => {
// Wrap in container
change.wrapBlock({
type,
data: Data.create(data)
},
{ normalize: false }
);

// Wrap in list items
selectedBlocks.forEach(node => {
if (isList(opts, node)) {
// Merge its items with the created list
node.nodes.forEach(({ key }) =>
change.unwrapNodeByKey(key, { normalize: false })
);
} else {
change.wrapBlockByKey(node.key, opts.typeItem, {
normalize: false
});
}
});

// Wrap in list items
selectedBlocks.forEach(node => {
if (isList(opts, node)) {
// Merge its items with the created list
node.nodes.forEach(({ key }) => change.unwrapNodeByKey(key));
} else {
change.wrapBlockByKey(node.key, opts.typeItem);
}
});
});

return change.normalize();
Expand All @@ -51,8 +46,8 @@ function getHighestSelectedBlocks(value: Value): List<Block> {
const range = value.selection;
const { document } = value;

const startBlock = document.getClosestBlock(range.startKey);
const endBlock = document.getClosestBlock(range.endKey);
const startBlock = document.getClosestBlock(range.start.key);
const endBlock = document.getClosestBlock(range.end.key);

if (startBlock === endBlock) {
return List([startBlock]);
Expand All @@ -61,7 +56,7 @@ function getHighestSelectedBlocks(value: Value): List<Block> {
const startPath = ancestor.getPath(startBlock.key);
const endPath = ancestor.getPath(endBlock.key);

return ancestor.nodes.slice(startPath[0], endPath[0] + 1);
return ancestor.nodes.slice(startPath.get(0), endPath.get(0) + 1);
}

export default wrapInList;
4 changes: 2 additions & 2 deletions lib/core.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import Options, { type OptionsFormat } from './options';
import { schema, validateNode } from './validation';
import { schema, normalizeNode } from './validation';
import {
wrapInList,
unwrapList,
Expand Down Expand Up @@ -33,7 +33,7 @@ function core(

return {
schema: schema(opts),
validateNode: validateNode(opts),
normalizeNode: normalizeNode(opts),

utils: {
getCurrentItem: getCurrentItem.bind(null, opts),
Expand Down
9 changes: 5 additions & 4 deletions lib/handlers/onBackspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ function onBackspace(
opts: Options
): void | any {
const { value } = change;
const { startOffset, selection } = value;
const { selection } = value;
const { start, isCollapsed, isExpanded } = selection;

// Only unwrap...
// ... with a collapsed selection
if (selection.isExpanded) {
if (isExpanded) {
return undefined;
}

// ... when at the beginning of nodes
if (startOffset > 0) {
if (start.offset > 0) {
return undefined;
}
// ... in a list
Expand All @@ -33,7 +34,7 @@ function onBackspace(
return undefined;
}
// ... more precisely at the beginning of the current item
if (!selection.isAtStartOf(currentItem)) {
if (!isCollapsed || !start.isAtStartOfNode(currentItem)) {
return undefined;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/handlers/onEnter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ function onEnter(
event.preventDefault();

// If expanded, delete first.
if (value.isExpanded) {
if (value.selection.isExpanded) {
change.delete();
}

if (currentItem.isEmpty) {
if (!value.schema.isVoid(currentItem) && currentItem.text === '') {
// Block is empty, we exit the list
if (getItemDepth(opts, value) > 1) {
return decreaseItemDepth(opts, change);
Expand Down
8 changes: 3 additions & 5 deletions lib/handlers/onTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@ import { getCurrentItem } from '../utils';
*/
function onTab(event: *, change: Change, editor: *, opts: Options): void | any {
const { value } = change;
const { isCollapsed } = value;
const { isCollapsed } = value.selection;

if (!isCollapsed || !getCurrentItem(opts, value)) {
return undefined;
}

event.preventDefault();

// Shift+tab reduce depth
if (event.shiftKey) {
event.preventDefault();

return decreaseItemDepth(opts, change);
}

// Tab increases depth
event.preventDefault();

return increaseItemDepth(opts, change);
}

Expand Down
1 change: 0 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ function EditList(

return {
...corePlugin,

onKeyDown: onKeyDown.bind(null, opts)
};
}
Expand Down
3 changes: 3 additions & 0 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ class Options extends Record({
}) {
// The possibles types for list containers
types: string[];

// The type of list items
typeItem: string;

// The type of default block in items
typeDefault: string;

// You can control here the automatic merging of adjacent lists
canMerge: (listA: Node, listB: Node) => boolean;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/getCurrentItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function getCurrentItem(opts: Options, value: Value, block?: Block): ?Block {
const { document } = value;

if (!block) {
if (!value.selection.startKey) return null;
if (!value.selection.start.key) return null;
block = value.startBlock;
}

Expand Down
13 changes: 8 additions & 5 deletions lib/utils/getItemsAtRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ function getItemsAtRange(
): List<Block> {
range = range || value.selection;

if (!range.startKey) {
if (!range.start.key) {
return List();
}

const { document } = value;

const startBlock = document.getClosestBlock(range.startKey);
const endBlock = document.getClosestBlock(range.endKey);
const startBlock = document.getClosestBlock(range.start.key);
const endBlock = document.getClosestBlock(range.end.key);

if (startBlock === endBlock) {
const item = getCurrentItem(opts, value, startBlock);
Expand All @@ -39,11 +39,14 @@ function getItemsAtRange(
const startPath = ancestor.getPath(startBlock.key);
const endPath = ancestor.getPath(endBlock.key);

return ancestor.nodes.slice(startPath[0], endPath[0] + 1);
} else if (ancestor.type === opts.typeItem) {
return ancestor.nodes.slice(startPath.get(0), endPath.get(0) + 1);
}

if (ancestor.type === opts.typeItem) {
// The ancestor is the highest list item that covers the range
return List([ancestor]);
}

// No list of items can cover the range
return List();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/validation/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import schema from './schema';
import validateNode from './validateNode';
import normalizeNode from './normalizeNode';

export { schema, validateNode };
export { schema, normalizeNode };
Loading