-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2141 from tf/master
Add sub and sup marks to editable text
- Loading branch information
Showing
13 changed files
with
233 additions
and
25 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
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,10 @@ | ||
de: | ||
pageflow_scrolled: | ||
inline_editing: | ||
formats: | ||
bold: Fett (Strg+B) | ||
italic: Kursiv (Strg+I) | ||
strikethrough: Durchgestrichen (Strg+Umschalten+S) | ||
underline: Unterstrichen (Strg+U) | ||
sub: Tiefgestellt (Strg+;) | ||
sup: Hochgestellt (Strg+,) |
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,10 @@ | ||
en: | ||
pageflow_scrolled: | ||
inline_editing: | ||
formats: | ||
bold: Bold (Ctrl+B) | ||
italic: Italic (Ctrl+I) | ||
strikethrough: Strikethrough (Ctrl+Shift+S) | ||
underline: Underline (Ctrl+U) | ||
sub: Subscript (Ctrl+;) | ||
sup: Superscript (Ctrl+,) |
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
100 changes: 100 additions & 0 deletions
100
entry_types/scrolled/package/spec/frontend/inlineEditing/EditableText/marks-spec.js
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,100 @@ | ||
/** @jsx jsx */ | ||
import { | ||
toggleMark, | ||
isMarkActive | ||
} from 'frontend/inlineEditing/EditableText/marks'; | ||
|
||
import {createHyperscript} from 'slate-hyperscript'; | ||
|
||
const h = createHyperscript({ | ||
elements: { | ||
paragraph: {type: 'paragraph'}, | ||
}, | ||
}); | ||
|
||
// Strip meta tags to make deep equality checks work | ||
const jsx = (tagName, attributes, ...children) => { | ||
delete attributes.__self; | ||
delete attributes.__source; | ||
return h(tagName, attributes, ...children); | ||
} | ||
|
||
describe('isMarkActive', () => { | ||
it('returns true if current node has mark', () => { | ||
const editor = ( | ||
<editor> | ||
<paragraph> | ||
<text bold>Line 1<cursor /></text> | ||
</paragraph> | ||
</editor> | ||
); | ||
|
||
expect(isMarkActive(editor, 'bold')).toEqual(true); | ||
expect(isMarkActive(editor, 'italic')).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe('toggleMark', () => { | ||
it('adds mark', () => { | ||
const editor = ( | ||
<editor> | ||
<paragraph> | ||
Some <anchor />text<focus /> | ||
</paragraph> | ||
</editor> | ||
); | ||
|
||
toggleMark(editor, 'bold'); | ||
|
||
const output = ( | ||
<editor> | ||
<paragraph> | ||
Some <text bold>text</text> | ||
</paragraph> | ||
</editor> | ||
); | ||
expect(editor.children).toEqual(output.children); | ||
}); | ||
|
||
it('removes mark', () => { | ||
const editor = ( | ||
<editor> | ||
<paragraph> | ||
Some <text bold><anchor />text<focus /></text> | ||
</paragraph> | ||
</editor> | ||
); | ||
|
||
toggleMark(editor, 'bold'); | ||
|
||
const output = ( | ||
<editor> | ||
<paragraph> | ||
Some text | ||
</paragraph> | ||
</editor> | ||
); | ||
expect(editor.children).toEqual(output.children); | ||
}); | ||
|
||
it('treats sub and sup as mutually exclusive', () => { | ||
const editor = ( | ||
<editor> | ||
<paragraph> | ||
Some <text sup><anchor />text<focus /></text> | ||
</paragraph> | ||
</editor> | ||
); | ||
|
||
toggleMark(editor, 'sub'); | ||
|
||
const output = ( | ||
<editor> | ||
<paragraph> | ||
Some <text sub>text</text> | ||
</paragraph> | ||
</editor> | ||
); | ||
expect(editor.children).toEqual(output.children); | ||
}); | ||
}); |
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
26 changes: 26 additions & 0 deletions
26
entry_types/scrolled/package/src/frontend/inlineEditing/EditableText/marks.js
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,26 @@ | ||
import {Editor} from 'slate'; | ||
|
||
const mutuallyExclusive = { | ||
sup: 'sub', | ||
sub: 'sup' | ||
} | ||
|
||
export function toggleMark(editor, format) { | ||
const isActive = isMarkActive(editor, format) | ||
|
||
if (isActive) { | ||
Editor.removeMark(editor, format) | ||
} else { | ||
if (mutuallyExclusive[format] && | ||
isMarkActive(editor, mutuallyExclusive[format])) { | ||
Editor.removeMark(editor, mutuallyExclusive[format]); | ||
} | ||
|
||
Editor.addMark(editor, format, true) | ||
} | ||
} | ||
|
||
export function isMarkActive(editor, format) { | ||
const marks = Editor.marks(editor) | ||
return marks ? marks[format] === true : false | ||
} |
35 changes: 35 additions & 0 deletions
35
entry_types/scrolled/package/src/frontend/inlineEditing/EditableText/shortcuts.js
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,35 @@ | ||
import {useCallback} from 'react'; | ||
import {toggleMark} from './marks'; | ||
|
||
export function useShortcutHandler(editor) { | ||
return useCallback(event => { | ||
if (!event.ctrlKey) { | ||
return; | ||
} | ||
|
||
if (event.key === 'b') { | ||
event.preventDefault() | ||
toggleMark(editor, 'bold'); | ||
} | ||
else if (event.key === 'i') { | ||
event.preventDefault() | ||
toggleMark(editor, 'italic'); | ||
} | ||
else if (event.key === 'u') { | ||
event.preventDefault() | ||
toggleMark(editor, 'underline'); | ||
} | ||
else if (event.key === 'S') { | ||
event.preventDefault() | ||
toggleMark(editor, 'strikethrough'); | ||
} | ||
else if (event.key === ',') { | ||
event.preventDefault() | ||
toggleMark(editor, 'sup'); | ||
} | ||
else if (event.key === ';') { | ||
event.preventDefault() | ||
toggleMark(editor, 'sub'); | ||
} | ||
}, [editor]); | ||
} |
1 change: 1 addition & 0 deletions
1
entry_types/scrolled/package/src/frontend/inlineEditing/images/sub.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions
1
entry_types/scrolled/package/src/frontend/inlineEditing/images/sup.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.