Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

refactor(lib/rich-text): delete excess code and correct structure #24

Merged
merged 2 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions src/lib/rich-text/extensions/code-plugin/changes/dedent-lines.js

This file was deleted.

16 changes: 1 addition & 15 deletions src/lib/rich-text/extensions/code-plugin/changes/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
import { indentLines } from "./indent-lines"
import { dedentLines } from "./dedent-lines"
import { unwrapCodeBlock } from "./unwrap-code-block"
import { unwrapCodeBlockByKey } from "./unwrap-code-block-by-key"
import { wrapCodeBlock } from "./wrap-code-block"
import { wrapCodeBlockByKey } from "./wrap-code-block-by-key"

export {
dedentLines,
indentLines,
unwrapCodeBlock,
unwrapCodeBlockByKey,
wrapCodeBlock,
wrapCodeBlockByKey,
}
export { indentLines } from "./indent-lines"

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

36 changes: 0 additions & 36 deletions src/lib/rich-text/extensions/code-plugin/core.js

This file was deleted.

1 change: 0 additions & 1 deletion src/lib/rich-text/extensions/code-plugin/handlers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export { onTab } from "./on-tab"
export { onShiftTab } from "./on-shift-tab"
export { onEnter } from "./on-enter"
export { onModEnter } from "./on-mod-enter"
export { onBackspace } from "./on-backspace"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getCurrentIndent, getCurrentCode } from "../utils"
import { getCurrentIndent } from "../utils"
import { getCurrentCode } from "../../common/utils"

/**
* User pressed Delete in an editor:
Expand Down Expand Up @@ -31,12 +32,11 @@ export function onBackspace(opts, event, change, editor) {
}
if (opts.exitBlockType) {
// Otherwise check if we are in an empty code container...
const currentCode = getCurrentCode(opts, value)
const codeBlock = getCurrentCode(opts, value)
const isStartOfCode =
selection.start.offset === 0 && currentCode.getFirstText() === startText
// PERF: avoid checking for whole currentCode.text
const isEmpty =
currentCode.nodes.size === 1 && currentLine.text.length === 0
selection.start.offset === 0 && codeBlock.getFirstText() === startText
// PERF: avoid checking for whole codeBlock.text
const isEmpty = codeBlock.nodes.size === 1 && currentLine.text.length === 0

if (isStartOfCode && isEmpty) {
event.preventDefault()
Expand Down
13 changes: 4 additions & 9 deletions src/lib/rich-text/extensions/code-plugin/handlers/on-key-down.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { isKeyHotkey } from "is-hotkey"
import { getCurrentCode } from "../utils"
import { getCurrentCode } from "../../common/utils"
import { onTab } from "./on-tab"
import { onShiftTab } from "./on-shift-tab"
import { onEnter } from "./on-enter"
import { onModEnter } from "./on-mod-enter"
import { onBackspace } from "./on-backspace"
import { onSelectAll } from "./on-select-all"

const isModA = isKeyHotkey("mod+a")
const isShiftTab = isKeyHotkey("shift+tab")
const isTab = isKeyHotkey("tab")
const isModZ = isKeyHotkey("mod+z")
const isModEnter = isKeyHotkey("mod+enter")
Expand All @@ -20,11 +18,11 @@ const isBackspace = isKeyHotkey("backspace")
*/
export function onKeyDown(opts, event, change, editor) {
const { value } = change
const currentCode = getCurrentCode(opts, value)
const codeBlock = getCurrentCode(opts, value)

// Inside code ?

if (!currentCode) {
if (!codeBlock) {
return editor()
}

Expand All @@ -35,10 +33,7 @@ export function onKeyDown(opts, event, change, editor) {
if (opts.selectAll && isModA(event)) {
return onSelectAll(...args)
}
if (isShiftTab(event)) {
// User is pressing Shift+Tab
return onShiftTab(...args)
}

if (isTab(event)) {
// User is pressing Tab
return onTab(...args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ export function onModEnter(opts, event, change, editor) {

event.preventDefault()

// Default behavior: insert an exit block
const range = change.value.selection

const exitBlock = Block.create({
type: "paragraph",
nodes: [Text.create()],
})

change.deleteAtRange(range, { normalize: false })
change.insertBlockAtRange(selection, exitBlock, {
normalize: false,
})
Expand Down
7 changes: 4 additions & 3 deletions src/lib/rich-text/extensions/code-plugin/handlers/on-paste.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { Document } from "slate"
import { getEventTransfer } from "slate-react"
import { getCurrentCode, deserializeCode } from "../utils"
import { deserializeCode } from "../utils"
import { getCurrentCode } from "../../common/utils"

/**
* User is pasting content, insert it as text
*/
export function onPaste(opts, event, change, editor) {
const { value } = change
const data = getEventTransfer(event)
const currentCode = getCurrentCode(opts, value)
const codeBlock = getCurrentCode(opts, value)

// Only handle paste when selection is completely a code block
const { endBlock } = value

if (!currentCode || !currentCode.hasDescendant(endBlock.key)) {
if (!codeBlock || !codeBlock.hasDescendant(endBlock.key)) {
return editor()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { getCurrentCode } from "../utils"
import { getCurrentCode } from "../../common/utils"

/**
* User is Cmd+A to select all text
*/
export function onSelectAll(opts, event, change, editor) {
export function onSelectAll(opts, event, change) {
const { value } = change

event.preventDefault()

const currentCode = getCurrentCode(opts, value)
const codeBlock = getCurrentCode(opts, value)

return change
.moveToStartOfNode(currentCode.getFirstText())
.moveFocusToEndOfNode(currentCode.getLastText())
.moveToStartOfNode(codeBlock.getFirstText())
.moveFocusToEndOfNode(codeBlock.getLastText())
}
18 changes: 0 additions & 18 deletions src/lib/rich-text/extensions/code-plugin/handlers/on-shift-tab.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { indentLines } from "../changes"
* User pressed Tab in an editor:
* Insert a tab after detecting it from code block content.
*/
export function onTab(opts, event, change, editor) {
export function onTab(opts, event, change) {
const { value } = change

event.preventDefault()
Expand Down
Loading