Skip to content

Commit

Permalink
feat: supports actions position (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
hangCode2001 authored Dec 10, 2022
1 parent 9c9b617 commit 6955a44
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
12 changes: 9 additions & 3 deletions packages/bytemd/src/editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
createEditorUtils,
findStartIndex,
getBuiltinActions,
getBuiltinRightActions,
handleImageUpload,
} from './editor'
import Help from './help.svelte'
Expand Down Expand Up @@ -106,7 +107,7 @@
keyMap = {}
// TODO: nested shortcuts
actions.forEach(({ handler }) => {
actions.leftActions.forEach(({ handler }) => {
if (handler?.type === 'action' && handler.shortcut) {
keyMap[handler.shortcut] = () => {
handler.click(context)
Expand Down Expand Up @@ -349,8 +350,9 @@
{activeTab}
{sidebar}
{fullscreen}
rightAfferentActions={actions.rightActions}
locale={mergedLocale}
{actions}
actions={actions.leftActions}
on:key={(e) => {
editor.setOption('keyMap', e.detail)
editor.focus()
Expand Down Expand Up @@ -408,7 +410,11 @@
>
{@html icons.Close({})}
</div>
<Help locale={mergedLocale} {actions} visible={sidebar === 'help'} />
<Help
locale={mergedLocale}
actions={actions.leftActions}
visible={sidebar === 'help'}
/>
<Toc
{hast}
locale={mergedLocale}
Expand Down
19 changes: 14 additions & 5 deletions packages/bytemd/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ export function getBuiltinActions(
locale: BytemdLocale,
plugins: BytemdPlugin[],
uploadImages: EditorProps['uploadImages']
): BytemdAction[] {
const items: BytemdAction[] = [
): { leftActions: BytemdAction[]; rightActions: BytemdAction[] } {
const leftActions: BytemdAction[] = [
{
icon: icons.H({}),
handler: {
Expand Down Expand Up @@ -339,9 +339,18 @@ export function getBuiltinActions(
cheatsheet: '---',
},
]

const rightActions: BytemdAction[] = []
plugins.forEach(({ actions }) => {
if (actions) items.push(...actions)
if (actions) {
actions.forEach((action) => {
if (!action.position || action.position !== 'right')
leftActions.push(action)
else rightActions.push(action)
})
}
})
return items
return {
leftActions,
rightActions,
}
}
2 changes: 2 additions & 0 deletions packages/bytemd/src/toolbar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
export let sidebar: false | 'help' | 'toc'
export let locale: BytemdLocale
export let actions: BytemdAction[]
export let rightAfferentActions: BytemdAction[]
interface RightAction extends BytemdAction {
active?: boolean
Expand Down Expand Up @@ -131,6 +132,7 @@
},
},
},
...rightAfferentActions
] as RightAction[]
const tippyClass = 'bytemd-tippy'
Expand Down
6 changes: 6 additions & 0 deletions packages/bytemd/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ export interface BytemdAction {
* Action title
*/
title?: string
/**
* Action icon position
*
* Specifies that the action icon is in the toolbar position, which defaults to the left
*/
position?: 'left' | 'right'
/**
* Action icon (16x16), usually inline svg
*/
Expand Down

0 comments on commit 6955a44

Please sign in to comment.