-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Trigger is "space" * "*" -> ul>li * "1" and "1." -> ol>li * "##" -> h2 * "###" -> h3 fixes #87
- Loading branch information
Showing
13 changed files
with
347 additions
and
53 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 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,92 @@ | ||
import Keycodes from '../utils/keycodes'; | ||
import Key from '../utils/key'; | ||
import { detect } from '../utils/array-utils'; | ||
import { MARKUP_SECTION_TYPE } from '../models/markup-section'; | ||
|
||
const { SPACE } = Keycodes; | ||
|
||
function replaceWithListSection(editor, listTagName) { | ||
const {head: {section}} = editor.cursor.offsets; | ||
|
||
const newSection = editor.run(postEditor => { | ||
const {builder} = postEditor; | ||
const listItem = builder.createListItem(); | ||
const listSection = builder.createListSection(listTagName, [listItem]); | ||
|
||
postEditor.replaceSection(section, listSection); | ||
return listItem; | ||
}); | ||
|
||
editor.cursor.moveToSection(newSection); | ||
} | ||
|
||
function replaceWithHeaderSection(editor, headingTagName) { | ||
const {head: {section}} = editor.cursor.offsets; | ||
|
||
const newSection = editor.run(postEditor => { | ||
const {builder} = postEditor; | ||
const newSection = builder.createMarkupSection(headingTagName); | ||
postEditor.replaceSection(section, newSection); | ||
return newSection; | ||
}); | ||
|
||
editor.cursor.moveToSection(newSection); | ||
} | ||
|
||
export function validateExpansion(expansion) { | ||
return !!expansion.trigger && !!expansion.text && !!expansion.run; | ||
} | ||
|
||
export const DEFAULT_TEXT_EXPANSIONS = [ | ||
{ | ||
trigger: SPACE, | ||
text: '*', | ||
run: (editor) => { | ||
replaceWithListSection(editor, 'ul'); | ||
} | ||
}, | ||
{ | ||
trigger: SPACE, | ||
text: '1', | ||
run: (editor) => { | ||
replaceWithListSection(editor, 'ol'); | ||
} | ||
}, | ||
{ | ||
trigger: SPACE, | ||
text: '1.', | ||
run: (editor) => { | ||
replaceWithListSection(editor, 'ol'); | ||
} | ||
}, | ||
{ | ||
trigger: SPACE, | ||
text: '##', | ||
run: (editor) => { | ||
replaceWithHeaderSection(editor, 'h2'); | ||
} | ||
}, | ||
{ | ||
trigger: SPACE, | ||
text: '###', | ||
run: (editor) => { | ||
replaceWithHeaderSection(editor, 'h3'); | ||
} | ||
} | ||
]; | ||
|
||
export function findExpansion(expansions, keyEvent, editor) { | ||
const key = Key.fromEvent(keyEvent); | ||
if (!key.isPrintable()) { return; } | ||
|
||
const {head:{section, offset}} = editor.cursor.offsets; | ||
if (section.type !== MARKUP_SECTION_TYPE) { return; } | ||
|
||
// FIXME this is potentially expensive to calculate and might be better | ||
// perf to first find expansions matching the trigger and only if matches | ||
// are found then calculating the _text | ||
const _text = section.textUntil(offset); | ||
return detect( | ||
expansions, | ||
({trigger, text}) => key.keyCode === trigger && _text === text); | ||
} |
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 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 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 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
Oops, something went wrong.