mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-11 01:33:58 +02:00
add preventDefault to all examples
This commit is contained in:
@@ -88,15 +88,15 @@ class App extends React.Component {
|
|||||||
// Return with no changes if it's not the "7" key with shift pressed.
|
// Return with no changes if it's not the "7" key with shift pressed.
|
||||||
if (event.which != 55 || !event.shiftKey) return
|
if (event.which != 55 || !event.shiftKey) return
|
||||||
|
|
||||||
// Otherwise, transform the state by inserting "and" at the cursor's position.
|
// Prevent the ampersand character from being inserted.
|
||||||
|
event.preventDefault()
|
||||||
|
|
||||||
|
// Transform the state by inserting "and" at the cursor's position.
|
||||||
const newState = state
|
const newState = state
|
||||||
.transform()
|
.transform()
|
||||||
.insertText('and')
|
.insertText('and')
|
||||||
.apply()
|
.apply()
|
||||||
|
|
||||||
// Prevent the ampersand character from being inserted.
|
|
||||||
event.preventDefault()
|
|
||||||
|
|
||||||
// Return the new state, which will cause the editor to update it.
|
// Return the new state, which will cause the editor to update it.
|
||||||
return newState
|
return newState
|
||||||
}
|
}
|
||||||
|
@@ -29,6 +29,7 @@ class App extends React.Component {
|
|||||||
|
|
||||||
onKeyDown = (event, data, state) => {
|
onKeyDown = (event, data, state) => {
|
||||||
if (event.which != 192 || !event.metaKey) return
|
if (event.which != 192 || !event.metaKey) return
|
||||||
|
event.preventDefault()
|
||||||
const isCode = state.blocks.some(block => block.type == 'code')
|
const isCode = state.blocks.some(block => block.type == 'code')
|
||||||
|
|
||||||
return state
|
return state
|
||||||
@@ -77,6 +78,7 @@ class App extends React.Component {
|
|||||||
switch (event.which) {
|
switch (event.which) {
|
||||||
// When "B" is pressed, add a "bold" mark to the text.
|
// When "B" is pressed, add a "bold" mark to the text.
|
||||||
case 66: {
|
case 66: {
|
||||||
|
event.preventDefault()
|
||||||
return state
|
return state
|
||||||
.transform()
|
.transform()
|
||||||
.addMark('bold')
|
.addMark('bold')
|
||||||
@@ -85,6 +87,7 @@ class App extends React.Component {
|
|||||||
// When "`" is pressed, keep our existing code block logic.
|
// When "`" is pressed, keep our existing code block logic.
|
||||||
case 192: {
|
case 192: {
|
||||||
const isCode = state.blocks.some(block => block.type == 'code')
|
const isCode = state.blocks.some(block => block.type == 'code')
|
||||||
|
event.preventDefault()
|
||||||
return state
|
return state
|
||||||
.transform()
|
.transform()
|
||||||
.setBlock(isCode ? 'paragraph' : 'code')
|
.setBlock(isCode ? 'paragraph' : 'code')
|
||||||
@@ -151,6 +154,7 @@ class App extends React.Component {
|
|||||||
|
|
||||||
switch (event.which) {
|
switch (event.which) {
|
||||||
case 66: {
|
case 66: {
|
||||||
|
event.preventDefault()
|
||||||
return state
|
return state
|
||||||
.transform()
|
.transform()
|
||||||
.toggleMark('bold')
|
.toggleMark('bold')
|
||||||
@@ -158,6 +162,7 @@ class App extends React.Component {
|
|||||||
}
|
}
|
||||||
case 192: {
|
case 192: {
|
||||||
const isCode = state.blocks.some(block => block.type == 'code')
|
const isCode = state.blocks.some(block => block.type == 'code')
|
||||||
|
event.preventDefault()
|
||||||
return state
|
return state
|
||||||
.transform()
|
.transform()
|
||||||
.setBlock(isCode ? 'paragraph' : 'code')
|
.setBlock(isCode ? 'paragraph' : 'code')
|
||||||
|
@@ -25,6 +25,8 @@ class App extends React.Component {
|
|||||||
onKeyDown = (event, data, state) => {
|
onKeyDown = (event, data, state) => {
|
||||||
if (event.which != 55 || !event.shiftKey) return
|
if (event.which != 55 || !event.shiftKey) return
|
||||||
|
|
||||||
|
event.preventDefault()
|
||||||
|
|
||||||
const newState = state
|
const newState = state
|
||||||
.transform()
|
.transform()
|
||||||
.insertText('and')
|
.insertText('and')
|
||||||
@@ -91,6 +93,8 @@ class App extends React.Component {
|
|||||||
onKeyDown = (event, data, state) => {
|
onKeyDown = (event, data, state) => {
|
||||||
if (event.which != 55 || !event.shiftKey) return
|
if (event.which != 55 || !event.shiftKey) return
|
||||||
|
|
||||||
|
event.preventDefault()
|
||||||
|
|
||||||
const newState = state
|
const newState = state
|
||||||
.transform()
|
.transform()
|
||||||
.insertText('and')
|
.insertText('and')
|
||||||
@@ -140,6 +144,9 @@ class App extends React.Component {
|
|||||||
// Return with no changes if it's not the "`" key with cmd/ctrl pressed.
|
// Return with no changes if it's not the "`" key with cmd/ctrl pressed.
|
||||||
if (event.which != 192 || !event.metaKey) return
|
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".
|
// Otherwise, set the currently selected blocks type to "code".
|
||||||
return state
|
return state
|
||||||
.transform()
|
.transform()
|
||||||
@@ -188,6 +195,8 @@ class App extends React.Component {
|
|||||||
onKeyDown = (event, data, state) => {
|
onKeyDown = (event, data, state) => {
|
||||||
if (event.which != 192 || !event.metaKey) return
|
if (event.which != 192 || !event.metaKey) return
|
||||||
|
|
||||||
|
event.preventDefault()
|
||||||
|
|
||||||
// Determine whether any of the currently selected blocks are code blocks.
|
// Determine whether any of the currently selected blocks are code blocks.
|
||||||
const isCode = state.blocks.some(block => block.type == 'code')
|
const isCode = state.blocks.some(block => block.type == 'code')
|
||||||
|
|
||||||
|
@@ -31,6 +31,7 @@ class App extends React.Component {
|
|||||||
|
|
||||||
onKeyDown = (event, data, state) => {
|
onKeyDown = (event, data, state) => {
|
||||||
if (!event.metaKey || event.which != 66) return
|
if (!event.metaKey || event.which != 66) return
|
||||||
|
event.preventDefault()
|
||||||
return state
|
return state
|
||||||
.transform()
|
.transform()
|
||||||
.toggleMark('bold')
|
.toggleMark('bold')
|
||||||
@@ -76,6 +77,9 @@ function MarkHotkey(options) {
|
|||||||
// Check that the key pressed matches our `code` option.
|
// Check that the key pressed matches our `code` option.
|
||||||
if (!event.metaKey || event.which != code) return
|
if (!event.metaKey || event.which != code) return
|
||||||
|
|
||||||
|
// Prevent the default characters from being inserted.
|
||||||
|
event.preventDefault()
|
||||||
|
|
||||||
// Toggle the mark `type`.
|
// Toggle the mark `type`.
|
||||||
return state
|
return state
|
||||||
.transform()
|
.transform()
|
||||||
@@ -214,6 +218,7 @@ function MarkHotkey(options) {
|
|||||||
onKeyDown(event, data, state) {
|
onKeyDown(event, data, state) {
|
||||||
// Change the comparison to use the key name.
|
// Change the comparison to use the key name.
|
||||||
if (!event.metaKey || keycode(event.which) != key) return
|
if (!event.metaKey || keycode(event.which) != key) return
|
||||||
|
event.preventDefault()
|
||||||
return state
|
return state
|
||||||
.transform()
|
.transform()
|
||||||
.toggleMark(type)
|
.toggleMark(type)
|
||||||
|
Reference in New Issue
Block a user