-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
751 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'typeface-open-sans'; | ||
import React from 'react'; | ||
import { Editor } from '.'; | ||
|
||
const App: React.FC = () => { | ||
return ( | ||
<div className="App"> | ||
<Editor/> | ||
</div> | ||
); | ||
} | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React, { useState } from 'react'; | ||
import { Value } from 'slate'; | ||
import { Editor as SlateEditor } from 'slate-react'; | ||
import WordPlugin from '../plugins/word'; | ||
|
||
const initialState = Value.fromJSON({ | ||
document: { | ||
nodes: [ | ||
{ | ||
object: 'block', | ||
type: 'paragaph', | ||
nodes: [ | ||
{ | ||
object: 'text', | ||
text: 'Hello from the first block.' | ||
} | ||
] | ||
}, | ||
{ | ||
object: 'block', | ||
type: 'paragaph', | ||
nodes: [ | ||
{ | ||
object: 'text', | ||
text: 'This is another paragraph.' | ||
} | ||
] | ||
} | ||
] | ||
} | ||
}); | ||
|
||
const plugins = [ | ||
WordPlugin() | ||
] | ||
|
||
export interface EditorProps { | ||
} | ||
|
||
const Editor: React.FC<EditorProps> = () => { | ||
const [value, setValue] = useState( | ||
initialState | ||
); | ||
|
||
return ( | ||
<div className="c_editor"> | ||
<SlateEditor | ||
onChange={({ value }: { value: Value }) => { | ||
setValue(value) | ||
}} | ||
plugins={plugins} | ||
value={value} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default Editor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as App } from './App'; | ||
export { default as Editor } from './Editor'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
* { | ||
box-sizing: border-box; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
|
||
body { | ||
font-family: 'Open Sans', sans-serif; | ||
font-size: 16px; | ||
} | ||
|
||
.c_editor { | ||
border: 1px solid #ccc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Plugin, Range, Value } from 'slate'; | ||
|
||
export default function WordPlugin(): Plugin { | ||
return { | ||
onChange: (editor, next) => { | ||
const word = findWord(editor.value); | ||
console.log(word); | ||
if(word) { | ||
console.log(word.text); | ||
editor.select(word); | ||
} | ||
next(); | ||
} | ||
}; | ||
} | ||
|
||
interface TextRange extends Range { | ||
text?: string; | ||
} | ||
|
||
function findWord( | ||
value: Value | ||
): TextRange | null { | ||
if(!value.focusText) { | ||
return null; | ||
} | ||
|
||
const text = value.focusText.text; | ||
const focus = value.selection.focus; | ||
const offset = focus.offset; | ||
|
||
const start = text.lastIndexOf( | ||
' ', | ||
offset - 1 | ||
) + 1; | ||
|
||
let end = text.indexOf( | ||
' ', | ||
offset | ||
); | ||
|
||
if(end < 0) { | ||
end = text.length; | ||
} | ||
|
||
const path = focus.path.toArray(); | ||
|
||
const range: TextRange = Range.create({ | ||
anchor: { | ||
offset: start, | ||
path | ||
}, | ||
focus: { | ||
offset: end, | ||
path | ||
} | ||
}); | ||
|
||
range.text = text.substring( | ||
start, | ||
end | ||
); | ||
|
||
return range; | ||
} |
Oops, something went wrong.