Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editor: add alt arrow keyboard shortcut #7078

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
89 changes: 88 additions & 1 deletion packages/editor/src/extensions/key-map/key-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,92 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { Extension } from "@tiptap/core";
import { Editor, Extension } from "@tiptap/core";
import { isInTable } from "@tiptap/pm/tables";
import { CodeBlock } from "../code-block/index.js";
import { showLinkPopup } from "../../toolbar/popups/link-popup.js";
import { isListActive } from "../../utils/list.js";
import { findParentNodeOfType } from "../../utils/prosemirror.js";
import { Fragment, Node, Slice } from "@tiptap/pm/model";
import { ReplaceStep } from "@tiptap/pm/transform";
import { Selection } from "@tiptap/pm/state";
import { Callout } from "../callout/callout.js";

function mapChildren<T>(
node: Node | Fragment,
callback: (child: Node, index: number, frag: Fragment) => T
): T[] {
const array = [];
for (let i = 0; i < node.childCount; i++) {
array.push(
callback(node.child(i), i, node instanceof Fragment ? node : node.content)
);
}
return array;
}

/**
* implementation from https://discuss.prosemirror.net/t/keymap-to-move-a-line/3645/5
*/
function moveNode(editor: Editor, dir: "up" | "down") {
const isDown = dir === "down";
const { state } = editor;
if (!state.selection.empty) {
return false;
}

const { $from } = state.selection;
const type = $from.node().type;
if (!type) {
return false;
}

const currentResolved = findParentNodeOfType(type)(state.selection);
if (!currentResolved) {
return false;
}

const { node: currentNode } = currentResolved;
const parentDepth = currentResolved.depth - 1;
const parent = $from.node(parentDepth);
const parentPos = $from.start(parentDepth);

if (currentNode.type !== type) {
return false;
}

let arr = mapChildren(parent, (node) => node);
let index = arr.indexOf(currentNode);
let swapWith = isDown ? index + 1 : index - 1;
if (swapWith >= arr.length || swapWith < 0) {
return false;
}
if (swapWith === 0 && parent.type.name === Callout.name) {
return false;
}

const swapWithNodeSize = arr[swapWith].nodeSize;

[arr[index], arr[swapWith]] = [arr[swapWith], arr[index]];

let tr = state.tr;
let replaceStart = parentPos;
let replaceEnd = $from.end(parentDepth);

const slice = new Slice(Fragment.fromArray(arr), 0, 0);

tr = tr.step(new ReplaceStep(replaceStart, replaceEnd, slice, false));
tr = tr.setSelection(
Selection.near(
tr.doc.resolve(
isDown ? $from.pos + swapWithNodeSize : $from.pos - swapWithNodeSize
)
)
);
tr.scrollIntoView();
editor.view.dispatch(tr);
return true;
}

export const KeyMap = Extension.create({
name: "key-map",
Expand Down Expand Up @@ -70,6 +151,12 @@ export const KeyMap = Extension.create({
"Mod-k": ({ editor }) => {
showLinkPopup(editor);
return true;
},
"Alt-ArrowUp": ({ editor }) => {
return moveNode(editor, "up");
},
"Alt-ArrowDown": ({ editor }) => {
return moveNode(editor, "down");
}
};
}
Expand Down
Loading