1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-10 17:24:02 +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) => { onKeyDown = (event, data, state) => {
// 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
// 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 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

View File

@@ -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')

View File

@@ -24,6 +24,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()
@@ -90,6 +92,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()
@@ -139,6 +143,9 @@ class App extends React.Component {
onKeyDown = (event, data, state) => { onKeyDown = (event, data, state) => {
// 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
@@ -187,6 +194,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')

View File

@@ -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)