Skip to content
This repository has been archived by the owner on Nov 18, 2017. It is now read-only.

Switch to blocks/inlines from marks #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 16 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import React from 'react';
import { findDOMNode } from 'slate';

import Portal from './lib/Portal';
import {
Expand All @@ -23,7 +24,13 @@ const MentionsPlugin = (options?: Options): SlatePlugin => {

return {
schema: {
marks: {
nodes: {
default: ({ attributes, children }) => (
<span {...attributes}>{children}</span>
),
line: ({ attributes, children }) => (
<span {...attributes}>{children}</span>
),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part shouldn't be necessary if we got inlines to work for mentions.

mention: Mention,
},
},
Expand All @@ -43,8 +50,12 @@ const MentionsPlugin = (options?: Options): SlatePlugin => {
// Guard selectedIndex to be within the length of the suggestions
const selected =
(suggestions && selectedIndex % suggestions.length) || 0;

// const node = state.startBlock.getMarksAtRange(state.selection).findLast(mark => mark.type === 'mention');
// console.log(node)
// console.log(findDOMNode(node))
portal = (
<Portal node={state.endText}>
<Portal node={state.startBlock}>
<Suggestions
selected={selected}
suggestions={editor.props.suggestions}
Expand All @@ -66,7 +77,7 @@ const MentionsPlugin = (options?: Options): SlatePlugin => {
// If the user types an @ we add a mention mark if we're not already in one
case AT_SIGN: {
if (currentlyInMention(state)) return;
return state.transform().addMark('mention').apply();
return state.transform().insertBlock('mention').focus().apply();
}
// If we're in a mention and either space or enter are pressed
// jump out of the mention and insert a space
Expand All @@ -92,7 +103,8 @@ const MentionsPlugin = (options?: Options): SlatePlugin => {
.transform()
.deleteBackward(mentionLength)
.insertText(text)
.removeMark('mention')
.splitBlock()
.setBlock('default')
.insertText(' ')
.focus()
.apply();
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FIRST_MENTION } from './constants';

export const currentlyInMention = (state: Object) =>
state.marks.some(mark => mark.type === 'mention');
state.blocks.some(block => block.type === 'mention');

// Find the index of the nearest "@" going backwards in text from end index
export const nearestAt = (text: string, end: number) => {
Expand Down