1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-04-21 05:42:00 +02:00

add preventDefault to all examples

This commit is contained in:
Ian Storm Taylor 2017-02-16 10:16:19 -08:00
parent da10eaeca9
commit 77ffcaae66
4 changed files with 23 additions and 4 deletions

View File

@ -87,15 +87,15 @@ class App extends React.Component {
onKeyDown = (event, data, state) => {
// Return with no changes if it's not the "7" key with shift pressed.
if (event.which != 55 || !event.shiftKey) return
// Prevent the ampersand character from being inserted.
event.preventDefault()
// Otherwise, transform the state by inserting "and" at the cursor's position.
// Transform the state by inserting "and" at the cursor's position.
const newState = state
.transform()
.insertText('and')
.apply()
// Prevent the ampersand character from being inserted.
event.preventDefault()
// Return the new state, which will cause the editor to update it.
return newState

View File

@ -29,6 +29,7 @@ class App extends React.Component {
onKeyDown = (event, data, state) => {
if (event.which != 192 || !event.metaKey) return
event.preventDefault()
const isCode = state.blocks.some(block => block.type == 'code')
return state
@ -77,6 +78,7 @@ class App extends React.Component {
switch (event.which) {
// When "B" is pressed, add a "bold" mark to the text.
case 66: {
event.preventDefault()
return state
.transform()
.addMark('bold')
@ -85,6 +87,7 @@ class App extends React.Component {
// When "`" is pressed, keep our existing code block logic.
case 192: {
const isCode = state.blocks.some(block => block.type == 'code')
event.preventDefault()
return state
.transform()
.setBlock(isCode ? 'paragraph' : 'code')
@ -151,6 +154,7 @@ class App extends React.Component {
switch (event.which) {
case 66: {
event.preventDefault()
return state
.transform()
.toggleMark('bold')
@ -158,6 +162,7 @@ class App extends React.Component {
}
case 192: {
const isCode = state.blocks.some(block => block.type == 'code')
event.preventDefault()
return state
.transform()
.setBlock(isCode ? 'paragraph' : 'code')

View File

@ -24,6 +24,8 @@ class App extends React.Component {
onKeyDown = (event, data, state) => {
if (event.which != 55 || !event.shiftKey) return
event.preventDefault()
const newState = state
.transform()
@ -90,6 +92,8 @@ class App extends React.Component {
onKeyDown = (event, data, state) => {
if (event.which != 55 || !event.shiftKey) return
event.preventDefault()
const newState = state
.transform()
@ -139,6 +143,9 @@ class App extends React.Component {
onKeyDown = (event, data, state) => {
// Return with no changes if it's not the "`" key with cmd/ctrl pressed.
if (event.which != 192 || !event.metaKey) return
// Prevent the "`" from being inserted by default.
event.preventDefault()
// Otherwise, set the currently selected blocks type to "code".
return state
@ -187,6 +194,8 @@ class App extends React.Component {
onKeyDown = (event, data, state) => {
if (event.which != 192 || !event.metaKey) return
event.preventDefault()
// Determine whether any of the currently selected blocks are code blocks.
const isCode = state.blocks.some(block => block.type == 'code')

View File

@ -31,6 +31,7 @@ class App extends React.Component {
onKeyDown = (event, data, state) => {
if (!event.metaKey || event.which != 66) return
event.preventDefault()
return state
.transform()
.toggleMark('bold')
@ -76,6 +77,9 @@ function MarkHotkey(options) {
// Check that the key pressed matches our `code` option.
if (!event.metaKey || event.which != code) return
// Prevent the default characters from being inserted.
event.preventDefault()
// Toggle the mark `type`.
return state
.transform()
@ -214,6 +218,7 @@ function MarkHotkey(options) {
onKeyDown(event, data, state) {
// Change the comparison to use the key name.
if (!event.metaKey || keycode(event.which) != key) return
event.preventDefault()
return state
.transform()
.toggleMark(type)