8000 Add shortcuts for link menu and headings by chrischrischris · Pull Request #423 · adobe/da-live · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add shortcuts for link menu and headings #423

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

Merged
merged 1 commit into from
Apr 30, 2025
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
8 changes: 7 additions & 1 deletion blocks/edit/prose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {

// DA
import prose2aem from '../../shared/prose2aem.js';
import menu from './plugins/menu.js';
import menu, { linkItem, getHeadingKeymap } from './plugins/menu.js';
import imageDrop from './plugins/imageDrop.js';
import linkConverter from './plugins/linkConverter.js';
import sectionPasteHandler from './plugins/sectionPasteHandler.js';
Expand Down Expand Up @@ -242,6 +242,12 @@ export default function initProse({ path, permissions }) {
'Mod-y': yRedo,
'Mod-Shift-z': yRedo,
'Mod-Shift-l': toggleLibrary,
'Mod-k': (state, dispatch, view) => { // toggle link prompt
const linkMarkType = state.schema.marks.link;
const linkMenuItem = linkItem(linkMarkType);
return linkMenuItem.spec.run(state, dispatch, view);
},
...getHeadingKeymap(schema),
}),
keymap({
Tab: handleTableTab(1),
Expand Down
138 changes: 81 additions & 57 deletions blocks/edit/prose/plugins/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import openLibrary from '../../da-library/da-library.js';

import insertTable from '../table.js';

const linkPromptState = { lastPrompt: { isOpen: () => false } };

function canInsert(state, nodeType) {
const { $from } = state.selection;
// eslint-disable-next-line no-plusplus
Expand Down Expand Up @@ -113,11 +115,9 @@ function hasImageNode(contentArr) {
return contentArr.some((node) => node.type.name === 'image');
}

function linkItem(linkMarkType) {
export function linkItem(linkMarkType) {
const label = 'Edit link';

let lastPrompt = { isOpen: () => false };

return new MenuItem({
title: 'Add or Edit link',
label,
Expand All @@ -130,8 +130,8 @@ function linkItem(linkMarkType) {
&& !hasImageNode(selContent.content?.content[0]?.content?.content);
},
run(initialState, dispatch, view) {
if (lastPrompt.isOpen()) {
lastPrompt.close();
if (linkPromptState.lastPrompt.isOpen()) {
linkPromptState.lastPrompt.close();
return;
}

Expand Down Expand Up @@ -198,9 +198,15 @@ function linkItem(linkMarkType) {
view.focus();
};

lastPrompt = openPrompt({ title: label, fields, callback, saveOnClose: true });
lastPrompt.addEventListener('closed', () => {
dispatch(view.state.tr.removeMark(start, end, view.state.schema.marks.contextHighlightingMark).setMeta('addToHistory', false));
linkPromptState.lastPrompt = openPrompt(
{ title: label, fields, callback, saveOnClose: true },
);
linkPromptState.lastPrompt.addEventListener('closed', () => {
dispatch(view.state.tr.removeMark(
start,
end,
view.state.schema.marks.contextHighlightingMark,
).setMeta('addToHistory', false));
});
},
});
Expand Down Expand Up @@ -334,7 +340,7 @@ function codeBlockItem(codeBlockNode) {
});
}

export function blockquoteItem(codeBlockNode) {
function blockquoteItem(codeBlockNode) {
return wrapItem(codeBlockNode, {
title: 'Change to blockquote',
label: 'Blockquote',
Expand All @@ -352,6 +358,60 @@ function headingItem(headingNode, options) {
return blockTypeItem(headingNode, options);
}

function createBlockMenuItem(node, options) {
const {
type,
level,
title,
label,
column,
class: className,
} = options;
const attrs = type === 'heading' ? { level } : {};
const menuItem = type === 'heading' ? headingItem : blockTypeItem;

const menuOptions = {
title,
label,
attrs,
...(column && { column }),
...(className && { class: className }),
};

return menuItem(node, menuOptions);
}

export function getHeadingKeymap(schema) {
const headingNode = schema.nodes.heading;
const paragraphNode = schema.nodes.paragraph;

const keymap = {
'Mod-Alt-0': (state, dispatch) => {
const menuItem = createBlockMenuItem(paragraphNode, {
type: 'paragraph',
title: 'Change to paragraph',
label: 'P',
});
return menuItem.spec.run(state, dispatch);
},
};

// Add heading shortcuts H1-H6
[1, 2, 3, 4, 5, 6].forEach((level) => {
keymap[`Mod-Alt-${level}`] = (state, dispatch) => {
const menuItem = createBlockMenuItem(headingNode, {
type: 'heading',
level,
title: `Change to heading ${level}`,
label: `H${level}`,
});
return menuItem.spec.run(state, dispatch);
};
});

return keymap;
}

function markItem(markType, options) {
const passedOptions = { active(state) { return markActive(state, markType); } };
// eslint-disable-next-line no-restricted-syntax, guard-for-in
Expand All @@ -378,8 +438,18 @@ function getTableMenu() {
}

function getTextBlocks(marks, nodes) {
const headingItems = [1, 2, 3, 4, 5, 6].map((i) => createBlockMenuItem(nodes.heading, {
type: 'heading',
level: i,
title: `Change to H${i}`,
label: `H${i}`,
column: 2,
class: `menu-item-h${i}`,
}));

return [
blockTypeItem(nodes.paragraph, {
createBlockMenuItem(nodes.paragraph, {
type: 'paragraph',
title: 'Change to paragraph',
label: 'P',
column: 2,
Expand All @@ -406,53 +476,7 @@ function getTextBlocks(marks, nodes) {
class: 'edit-sub',
}),
codeMarkItem(marks.code),
// markItem(marks.s, {
// title: 'Toggle strikethrough',
// label: 'S',
// class: 'edit-s',
// }),
headingItem(nodes.heading, {
title: 'Change to H1',
label: 'H1',
column: 2,
attrs: { level: 1 },
class: 'menu-item-h1',
}),
headingItem(nodes.heading, {
title: 'Change to H2',
label: 'H2',
column: 2,
attrs: { level: 2 },
class: 'menu-item-h2',
}),
headingItem(nodes.heading, {
title: 'Change to h3',
label: 'h3',
column: 2,
attrs: { level: 3 },
class: 'menu-item-h3',
}),
headingItem(nodes.heading, {
title: 'Change to h4',
label: 'h4',
column: 2,
attrs: { level: 4 },
class: 'menu-item-h4',
}),
headingItem(nodes.heading, {
title: 'Change to h5',
label: 'h5',
column: 2,
attrs: { level: 5 },
class: 'menu-item-h5',
}),
headingItem(nodes.heading, {
title: 'Change to h6',
label: 'h6',
column: 2,
attrs: { level: 6 },
class: 'menu-item-h6',
}),
...headingItems,
blockquoteItem(nodes.blockquote),
codeBlockItem(nodes.code_block),
];
Expand Down
6 changes: 4 additions & 2 deletions blocks/edit/prose/plugins/slashMenu/keyAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ export function processKeyData(data) {
const blockMap = new Map();

data?.forEach((item) => {
const itemBlocks = item.blocks.toLowerCase().trim();
const blocks = itemBlocks.split(',').map((block) => block.trim());
const itemBlocks = item.blocks?.toLowerCase().trim();
const blocks = itemBlocks?.split(',').map((block) => block.trim());

if (!blocks) return;

const values = item.values.toLowerCase().split('|').map((v) => {
const [label, val] = v.split('=').map((vb) => vb.trim());
Expand Down
Loading
0