prosemirror-codemark
Advanced tools
Comparing version 0.3.0 to 0.3.1
import { MarkType } from 'prosemirror-model'; | ||
import { Plugin, Transaction } from 'prosemirror-state'; | ||
import { EditorState, Plugin, Transaction } from 'prosemirror-state'; | ||
import { EditorView } from 'prosemirror-view'; | ||
@@ -9,2 +9,3 @@ export declare function stepOutsideNextTrAndPass(view: EditorView, plugin: Plugin, action?: 'click' | 'next'): boolean; | ||
export declare function onBackspace(view: EditorView, plugin: Plugin, event: KeyboardEvent, markType: MarkType): boolean; | ||
export declare function stepOutside(tr: Transaction, markType: MarkType): Transaction | null; | ||
export declare function onDelete(view: EditorView, plugin: Plugin, event: KeyboardEvent, markType: MarkType): boolean; | ||
export declare function stepOutside(state: EditorState, markType: MarkType): Transaction | null; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.stepOutside = exports.onBackspace = exports.onArrowLeft = exports.onArrowRight = exports.onBacktick = exports.stepOutsideNextTrAndPass = void 0; | ||
exports.stepOutside = exports.onDelete = exports.onBackspace = exports.onArrowLeft = exports.onArrowRight = exports.onBacktick = exports.stepOutsideNextTrAndPass = void 0; | ||
const prosemirror_state_1 = require("prosemirror-state"); | ||
@@ -163,64 +163,48 @@ const utils_1 = require("./utils"); | ||
function onBackspace(view, plugin, event, markType) { | ||
var _a, _b; | ||
if (event.metaKey || event.shiftKey || event.altKey || event.ctrlKey) | ||
return false; | ||
const { selection, doc } = view.state; | ||
if (selection.empty && selection.$from.parentOffset === 0) { | ||
// No override at the start of the line! | ||
const from = doc.resolve(selection.from - 1); | ||
const fromCode = !!markType.isInSet(from.marks()); | ||
const startOfLine = from.parentOffset === 0; | ||
const toCode = !!markType.isInSet(doc.resolve(selection.to + 1).marks()); | ||
if ((!fromCode || startOfLine) && !toCode) { | ||
// `x|` → | | ||
// `|████` → | | ||
// `|███`█ → | | ||
return stepOutsideNextTrAndPass(view, plugin); | ||
} | ||
const inCode = !!markType.isInSet(selection.$from.marks()); | ||
// Index can be out of bounds in some schemas | ||
let nextCode; | ||
let plusCode; | ||
try { | ||
nextCode = !!markType.isInSet((_a = doc.resolve(selection.empty ? selection.from - 1 : selection.from + 1).marks()) !== null && _a !== void 0 ? _a : []); | ||
// Firefox has difficulty with the decorations on -1. | ||
const pluginState = plugin.getState(view.state); | ||
if (selection.empty && (pluginState === null || pluginState === void 0 ? void 0 : pluginState.side) === -1) { | ||
const tr = view.state.tr.delete(selection.from - 1, selection.from); | ||
view.dispatch(tr); | ||
return true; | ||
} | ||
catch (error) { | ||
nextCode = false; | ||
} | ||
try { | ||
plusCode = !!markType.isInSet((_b = doc.resolve(selection.empty ? selection.from + 1 : selection.to + 1).marks()) !== null && _b !== void 0 ? _b : []); | ||
} | ||
catch (error) { | ||
plusCode = false; | ||
} | ||
if (inCode === nextCode && (inCode === plusCode || !plusCode)) | ||
return false; | ||
} | ||
exports.onBackspace = onBackspace; | ||
function onDelete(view, plugin, event, markType) { | ||
if (event.metaKey || event.shiftKey || event.altKey || event.ctrlKey) | ||
return false; | ||
let { tr } = view.state; | ||
if (selection.empty) { | ||
tr = tr.delete(selection.from - 1, selection.from); | ||
const { selection, doc } = view.state; | ||
const fromCode = !!markType.isInSet(selection.$from.marks()); | ||
const startOfLine = selection.$from.parentOffset === 0; | ||
const toCode = !!markType.isInSet(doc.resolve(selection.to + 2).marks()); | ||
if ((!fromCode || startOfLine) && !toCode) { | ||
return stepOutsideNextTrAndPass(view, plugin); | ||
} | ||
else { | ||
tr = tr.delete(selection.from, selection.to); | ||
} | ||
if ((nextCode && selection.empty) || (inCode && !selection.empty)) { | ||
// `code`_|_ --> `code`| nextCode && selection.empty | ||
// `code`███ --> `code`| inCode && !selection.empty | ||
view.dispatch(tr.removeStoredMark(markType)); | ||
return true; | ||
} | ||
if (tr.selection.$from.parentOffset === 0) { | ||
// ^███`code` --> ^|`code` | ||
view.dispatch(tr.removeStoredMark(markType)); | ||
return true; | ||
} | ||
if (inCode || nextCode) { | ||
// `c|ode` --> `|ode` | ||
// `██de` --> `|de` | ||
view.dispatch(tr.addStoredMark(markType.create())); | ||
return true; | ||
} | ||
return false; | ||
} | ||
exports.onBackspace = onBackspace; | ||
function stepOutside(tr, markType) { | ||
var _a; | ||
if (!tr) | ||
exports.onDelete = onDelete; | ||
function stepOutside(state, markType) { | ||
var _a, _b; | ||
if (!state) | ||
return null; | ||
const { selection, doc } = tr; | ||
const { selection, doc } = state; | ||
if (!selection.empty) | ||
return null; | ||
const stored = !!markType.isInSet((_a = state.storedMarks) !== null && _a !== void 0 ? _a : []); | ||
const inCode = !!markType.isInSet(selection.$from.marks()); | ||
const nextCode = !!markType.isInSet((_a = doc.resolve(selection.from + 1).marks()) !== null && _a !== void 0 ? _a : []); | ||
const nextCode = !!markType.isInSet((_b = doc.resolve(selection.from + 1).marks()) !== null && _b !== void 0 ? _b : []); | ||
const startOfLine = selection.$from.parentOffset === 0; | ||
@@ -230,4 +214,4 @@ // `code|` --> `code`| | ||
// ^`|code` --> ^|`code` | ||
if (inCode !== nextCode || (inCode && startOfLine)) | ||
return tr.removeStoredMark(markType); | ||
if (inCode !== nextCode || (!inCode && stored !== inCode) || (inCode && startOfLine)) | ||
return state.tr.removeStoredMark(markType); | ||
return null; | ||
@@ -234,0 +218,0 @@ } |
@@ -30,3 +30,3 @@ "use strict"; | ||
if ((prev === null || prev === void 0 ? void 0 : prev.next) || (meta === null || meta === void 0 ? void 0 : meta.action) === 'click') { | ||
return (0, actions_1.stepOutside)(newState.tr, (0, utils_1.getMarkType)(newState, opts)); | ||
return (0, actions_1.stepOutside)(newState, (0, utils_1.getMarkType)(newState, opts)); | ||
} | ||
@@ -87,2 +87,4 @@ return null; | ||
return (0, actions_1.onBackspace)(view, plugin, event, (0, utils_1.getMarkType)(view, opts)); | ||
case 'Delete': | ||
return (0, actions_1.onDelete)(view, plugin, event, (0, utils_1.getMarkType)(view, opts)); | ||
case 'ArrowUp': | ||
@@ -89,0 +91,0 @@ case 'ArrowDown': |
{ | ||
"name": "prosemirror-codemark", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Inline code mark for ProseMirror", | ||
@@ -5,0 +5,0 @@ "author": "Rowan Cockett <rowan@curvenote.com>", |
@@ -131,16 +131,8 @@ # `prosemirror-codemark` | ||
### Backspace: | ||
### Backspace & Delete: | ||
- `` `code`x| → `code`| `` (delete, remain outside) | ||
- `` `code`|██ → `code`| `` (delete selected, remain outside) | ||
- `` `c|ode` → `|ode` `` (delete, remain inside) | ||
- `` `|██de` → `|de` `` (delete selected, remain inside) | ||
- `` `code`$| → `code`|$ `` (delete into line, remain outside) | ||
- `` ^███`code` --> ^|`code` `` (delete to start of line, remain outside) | ||
- TODO: `` `|████` → | `` (delete full mark, next insertion normal) | ||
- `` `x|` → | `` (delete text, next insertion normal) | ||
- `` `|████` → | `` (delete full mark, next insertion normal) | ||
- `` `|███`█ → | `` (delete full mark, next insertion normal) | ||
### Delete: | ||
- TODO! | ||
### Clicking | ||
@@ -147,0 +139,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
57083
539
153