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

feat!: injection chains to allow merging custom and addons/themes keyboard shortcuts #702

Merged
merged 4 commits into from
Sep 12, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/client/logic/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export function strokeShortcut(key: KeyFilter, fn: Fn) {
}

export function registerShortcuts() {
const { customShortcuts, defaultShortcuts } = setupShortcuts()
const allShortcuts = setupShortcuts()

const shortcuts = new Map<string | Ref<Boolean>, ShortcutOptions>(
[...defaultShortcuts, ...customShortcuts].map((options: ShortcutOptions) => [options.key, options]),
allShortcuts.map((options: ShortcutOptions) => [options.key, options]),
)

shortcuts.forEach((options) => {
Expand Down
24 changes: 19 additions & 5 deletions packages/client/setup/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export default function setupShortcuts() {
showGotoDialog: () => showGotoDialog.value = !showGotoDialog.value,
}

const injection_arg_2: ShortcutOptions[] = [
// eslint-disable-next-line prefer-const
let injection_return: ShortcutOptions[] = [
{ name: 'next_space', key: and(space, not(shift)), fn: next, autoRepeat: true },
{ name: 'prev_space', key: and(space, shift), fn: prev, autoRepeat: true },
{ name: 'next_right', key: and(right, not(shift), not(showOverview)), fn: next, autoRepeat: true },
Expand All @@ -50,10 +51,23 @@ export default function setupShortcuts() {
{ name: 'goto_from_overview', key: and(enter, showOverview), fn: () => { go(currentOverviewPage.value); showOverview.value = false } },
]

// eslint-disable-next-line prefer-const
let injection_return: Array<ShortcutOptions> = []
const baseShortcutNames = new Set(injection_return.map(s => s.name))

/* __chained_injections__ */

/* __injections__ */
const remainingBaseShortcutNames = injection_return.filter(s => s.name && baseShortcutNames.has(s.name))
if (remainingBaseShortcutNames.length === 0) {
const message = [
'========== WARNING ==========',
'defineShortcutsSetup did not return any of the base shortcuts.',
'See https://sli.dev/custom/config-shortcuts.html for migration.',
'If it is intentional, return at least one shortcut with one of the base names (e.g. name:"goto").',
].join('\n\n')
// eslint-disable-next-line no-alert
alert(message)

console.warn(message)
}

return { customShortcuts: injection_return, defaultShortcuts: injection_arg_2 }
return injection_return
}
41 changes: 16 additions & 25 deletions packages/slidev/node/plugins/setupClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export function createClientSetupPlugin({ clientRoot, themeRoots, addonRoots, us
const name = id.slice(setupEntry.length + 1)
const imports: string[] = []
const injections: string[] = []
const asyncInjections: string[] = []

const setups = uniq([
...themeRoots,
Expand All @@ -30,47 +29,39 @@ export function createClientSetupPlugin({ clientRoot, themeRoots, addonRoots, us

imports.push(`import __n${idx} from '${toAtFS(path)}'`)

let fn = `__n${idx}`
let awaitFn = `await __n${idx}`
let fn = `:AWAIT:__n${idx}`

if (/\binjection_return\b/g.test(code)) {
if (/\binjection_return\b/g.test(code))
fn = `injection_return = ${fn}`
awaitFn = `injection_return = ${awaitFn}`
}

if (/\binjection_arg\b/g.test(code)) {
fn += '('
awaitFn += '('

const matches = Array.from(code.matchAll(/\binjection_arg(_\d+)?\b/g))
const dedupedMatches = Array.from(new Set(matches.map(m => m[0])))
dedupedMatches.forEach((key, index) => {
const isLast = index === dedupedMatches.length - 1
const arg = key + (isLast ? '' : ',')
fn += arg
awaitFn += arg
})

fn += ')'
awaitFn += ')'
fn += dedupedMatches.join(', ')
fn += ', :LAST:)'
}
else {
fn += ('()')
awaitFn += ('()')
fn += '(:LAST:)'
}

injections.push(
`// ${path}`,
fn,
)
asyncInjections.push(
`// ${path}`,
awaitFn,
)
})

function getInjections(isAwait = false, isChained = false): string {
return injections.join('\n')
.replace(/:AWAIT:/g, isAwait ? 'await ' : '')
.replace(/(,\s*)?:LAST:/g, isChained ? '$1injection_return' : '')
}

code = code.replace('/* __imports__ */', imports.join('\n'))
code = code.replace('/* __injections__ */', injections.join('\n'))
code = code.replace('/* __async_injections__ */', asyncInjections.join('\n'))
code = code.replace('/* __injections__ */', getInjections())
code = code.replace('/* __async_injections__ */', getInjections(true))
code = code.replace('/* __chained_injections__ */', getInjections(false, true))
code = code.replace('/* __chained_async_injections__ */', getInjections(true, true))
return code
}

Expand Down