Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update slate to 0.44 #1

Open
wants to merge 5 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
25 changes: 13 additions & 12 deletions lib/changes/decreaseItemDepth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import { Block, type Change } from 'slate';
import { Block } from 'slate';
import { type Editor } from 'slate-react'

import type Options from '../options';
import { getItemDepth, getCurrentItem } from '../utils';
Expand All @@ -10,19 +11,19 @@ import { getItemDepth, getCurrentItem } from '../utils';
*
* No-op for root items.
*/
function decreaseItemDepth(opts: Options, change: Change): Change {
const { value } = change;
function decreaseItemDepth(opts: Options, editor: Editor): Editor {
const { value } = editor;
const { document } = value;

// Cannot decrease item depth of root items
const depth = getItemDepth(opts, value);
if (depth == 1) {
return change;
return editor;
}

const currentItem = getCurrentItem(opts, value);
if (!currentItem) {
return change;
return editor;
}

const currentList = document.getParent(currentItem.key);
Expand All @@ -46,30 +47,30 @@ function decreaseItemDepth(opts: Options, change: Change): Change {
data: currentList.data
});
// Add the sublist
change.withoutNormalizing(() => {
change.insertNodeByKey(
editor.withoutNormalizing(() => {
editor.insertNodeByKey(
currentItem.key,
currentItem.nodes.size,
sublist
);

change.moveNodeByKey(
editor.moveNodeByKey(
currentItem.key,
parentList.key,
parentList.nodes.indexOf(parentItem) + 1
);

// Move the followingItems to the sublist
followingItems.forEach((item, index) =>
change.moveNodeByKey(
editor.moveNodeByKey(
item.key,
sublist.key,
sublist.nodes.size + index
)
);
});
} else {
change.moveNodeByKey(
editor.moveNodeByKey(
currentItem.key,
parentList.key,
parentList.nodes.indexOf(parentItem) + 1
Expand All @@ -78,10 +79,10 @@ function decreaseItemDepth(opts: Options, change: Change): Change {

// Remove the currentList completely if needed
if (willEmptyCurrentList) {
change.removeNodeByKey(currentList.key);
editor.removeNodeByKey(currentList.key);
}

return change;
return editor;
}

export default decreaseItemDepth;
31 changes: 16 additions & 15 deletions lib/changes/increaseItemDepth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import { Block, type Change } from 'slate';
import { Block } from 'slate';
import { type Editor } from "slate-react";

import type Options from '../options';
import {
Expand All @@ -14,20 +15,20 @@ import {
* of previous item.
* For first items in a list, does nothing.
*/
function increaseItemDepth(opts: Options, change: Change): Change {
const previousItem = getPreviousItem(opts, change.value);
const currentItem = getCurrentItem(opts, change.value);
function increaseItemDepth(opts: Options, editor: Editor): Editor {
const previousItem = getPreviousItem(opts, editor.value);
const currentItem = getCurrentItem(opts, editor.value);

if (!previousItem) {
return change;
return editor;
}

if (!currentItem) {
return change;
return editor;
}

// Move the item in the sublist of previous item
return moveAsSubItem(opts, change, currentItem, previousItem.key);
return moveAsSubItem(opts, editor, currentItem, previousItem.key);
}

/**
Expand All @@ -36,27 +37,27 @@ function increaseItemDepth(opts: Options, change: Change): Change {
*/
function moveAsSubItem(
opts: Options,
change: Change,
editor: Editor,
// The list item to add
item: Block,
// The key of the destination node
destKey: string
): Change {
const destination = change.value.document.getDescendant(destKey);
): Editor {
const destination = editor.value.document.getDescendant(destKey);
const lastIndex = destination.nodes.size;
const lastChild = destination.nodes.last();

// The potential existing last child list
const existingList = isList(opts, lastChild) ? lastChild : null;

if (existingList) {
return change.moveNodeByKey(
return editor.moveNodeByKey(
item.key,
existingList.key,
existingList.nodes.size // as last item
);
}
const currentList = getListForItem(opts, change.value, destination);
const currentList = getListForItem(opts, editor.value, destination);
if (!currentList) {
throw new Error('Destination is not in a list');
}
Expand All @@ -67,9 +68,9 @@ function moveAsSubItem(
data: currentList.data
});

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

Expand Down
10 changes: 5 additions & 5 deletions lib/changes/splitListItem.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// @flow
import { type Change } from 'slate';
import { type Editor } from 'slate-react';

import type Options from '../options';
import { getCurrentItem } from '../utils';

/**
* Split a list item at the start of the current range.
*/
function splitListItem(opts: Options, change: Change): Change {
const { value } = change;
function splitListItem(opts: Options, editor: Editor): Editor {
const { value } = editor;
const currentItem = getCurrentItem(opts, value);
if (!currentItem) {
return change;
return editor;
}

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

return change.splitDescendantsByKey(
return editor.splitDescendantsByKey(
currentItem.key,
start.key,
splitOffset
Expand Down
20 changes: 10 additions & 10 deletions lib/changes/unwrapList.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
// @flow
import { type Change } from 'slate';
import { type Editor } from 'slate-react';

import type Options from '../options';
import { getItemsAtRange } from '../utils';

/**
* Unwrap items at range from their list.
*/
function unwrapList(opts: Options, change: Change): Change {
const items = getItemsAtRange(opts, change.value);
function unwrapList(opts: Options, editor: Editor): Editor {
const items = getItemsAtRange(opts, editor.value);

if (items.isEmpty()) {
return change;
return editor;
}

return change.withoutNormalizing(() => {
return editor.withoutNormalizing(() => {
// Unwrap the items from their list
items.forEach(item => change.unwrapNodeByKey(item.key));
items.forEach(item => editor.unwrapNodeByKey(item.key));

// Parent of the list of the items
const firstItem = items.first();
const parent = change.value.document.getParent(firstItem.key);
const parent = editor.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);
editor.moveNodeByKey(node.key, parent.key, index);
index += 1;
})
);

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

return change;
return editor;
});
}

Expand Down
19 changes: 10 additions & 9 deletions lib/changes/wrapInList.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import { Data, type Value, type Change, type Block } from 'slate';
import { Data, type Value, type Block } from 'slate';
import { type Editor } from 'slate-react';
import { List } from 'immutable';

import type Options from '../options';
Expand All @@ -11,16 +12,16 @@ import { isList } from '../utils';
*/
function wrapInList(
opts: Options,
change: Change,
editor: Editor,
type?: string,
data?: Object | Data
): Change {
const selectedBlocks = getHighestSelectedBlocks(change.value);
): Editor {
const selectedBlocks = getHighestSelectedBlocks(editor.value);
type = type || opts.types[0];

change.withoutNormalizing(() => {
editor.withoutNormalizing(() => {
// Wrap in container
change.wrapBlock({
editor.wrapBlock({
type,
data: Data.create(data)
});
Expand All @@ -29,14 +30,14 @@ function wrapInList(
selectedBlocks.forEach(node => {
if (isList(opts, node)) {
// Merge its items with the created list
node.nodes.forEach(({ key }) => change.unwrapNodeByKey(key));
node.nodes.forEach(({ key }) => editor.unwrapNodeByKey(key));
} else {
change.wrapBlockByKey(node.key, opts.typeItem);
editor.wrapBlockByKey(node.key, opts.typeItem);
}
});
});

return change.normalize();
return editor.normalize();
}

/**
Expand Down
16 changes: 7 additions & 9 deletions lib/handlers/onBackspace.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// @flow
import { type Change } from 'slate';

import type Options from '../options';
import { unwrapList } from '../changes';
import { getCurrentItem } from '../utils';
Expand All @@ -10,36 +8,36 @@ import { getCurrentItem } from '../utils';
*/
function onBackspace(
event: *,
change: Change,
editor: *,
next: *,
opts: Options
): void | any {
const { value } = change;
const { value } = editor;
const { selection } = value;
const { start, isCollapsed, isExpanded } = selection;

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

// ... when at the beginning of nodes
if (start.offset > 0) {
return undefined;
return next();
}
// ... in a list
const currentItem = getCurrentItem(opts, value);
if (!currentItem) {
return undefined;
return next();
}
// ... more precisely at the beginning of the current item
if (!isCollapsed || !start.isAtStartOfNode(currentItem)) {
return undefined;
return next();
}

event.preventDefault();
return unwrapList(opts, change);
return unwrapList(opts, editor);
}

export default onBackspace;
20 changes: 9 additions & 11 deletions lib/handlers/onEnter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// @flow
import { type Change } from 'slate';

import type Options from '../options';
import { unwrapList, splitListItem, decreaseItemDepth } from '../changes';
import { getCurrentItem, getItemDepth } from '../utils';
Expand All @@ -14,41 +12,41 @@ import { getCurrentItem, getItemDepth } from '../utils';
*/
function onEnter(
event: *,
change: Change,
editor: *,
next: *,
opts: Options
): void | any {
// Pressing Shift+Enter
// should split block normally
if (event.shiftKey) {
return undefined;
return next();
}

const { value } = change;
const { value } = editor;
const currentItem = getCurrentItem(opts, value);

// Not in a list
if (!currentItem) {
return undefined;
return next();
}

event.preventDefault();

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

if (!value.schema.isVoid(currentItem) && currentItem.text === '') {
if (!editor.isVoid(currentItem) && currentItem.text === '') {
// Block is empty, we exit the list
if (getItemDepth(opts, value) > 1) {
return decreaseItemDepth(opts, change);
return decreaseItemDepth(opts, editor);
}
// Exit list
return unwrapList(opts, change);
return unwrapList(opts, editor);
}
// Split list item
return splitListItem(opts, change);
return splitListItem(opts, editor);
}

export default onEnter;
Loading