Skip to content

Commit 6955a44

Browse files
authored
feat: supports actions position (#219)
1 parent 9c9b617 commit 6955a44

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

packages/bytemd/src/editor.svelte

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
createEditorUtils,
88
findStartIndex,
99
getBuiltinActions,
10+
getBuiltinRightActions,
1011
handleImageUpload,
1112
} from './editor'
1213
import Help from './help.svelte'
@@ -106,7 +107,7 @@
106107
107108
keyMap = {}
108109
// TODO: nested shortcuts
109-
actions.forEach(({ handler }) => {
110+
actions.leftActions.forEach(({ handler }) => {
110111
if (handler?.type === 'action' && handler.shortcut) {
111112
keyMap[handler.shortcut] = () => {
112113
handler.click(context)
@@ -349,8 +350,9 @@
349350
{activeTab}
350351
{sidebar}
351352
{fullscreen}
353+
rightAfferentActions={actions.rightActions}
352354
locale={mergedLocale}
353-
{actions}
355+
actions={actions.leftActions}
354356
on:key={(e) => {
355357
editor.setOption('keyMap', e.detail)
356358
editor.focus()
@@ -408,7 +410,11 @@
408410
>
409411
{@html icons.Close({})}
410412
</div>
411-
<Help locale={mergedLocale} {actions} visible={sidebar === 'help'} />
413+
<Help
414+
locale={mergedLocale}
415+
actions={actions.leftActions}
416+
visible={sidebar === 'help'}
417+
/>
412418
<Toc
413419
{hast}
414420
locale={mergedLocale}

packages/bytemd/src/editor.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ export function getBuiltinActions(
166166
locale: BytemdLocale,
167167
plugins: BytemdPlugin[],
168168
uploadImages: EditorProps['uploadImages']
169-
): BytemdAction[] {
170-
const items: BytemdAction[] = [
169+
): { leftActions: BytemdAction[]; rightActions: BytemdAction[] } {
170+
const leftActions: BytemdAction[] = [
171171
{
172172
icon: icons.H({}),
173173
handler: {
@@ -339,9 +339,18 @@ export function getBuiltinActions(
339339
cheatsheet: '---',
340340
},
341341
]
342-
342+
const rightActions: BytemdAction[] = []
343343
plugins.forEach(({ actions }) => {
344-
if (actions) items.push(...actions)
344+
if (actions) {
345+
actions.forEach((action) => {
346+
if (!action.position || action.position !== 'right')
347+
leftActions.push(action)
348+
else rightActions.push(action)
349+
})
350+
}
345351
})
346-
return items
352+
return {
353+
leftActions,
354+
rightActions,
355+
}
347356
}

packages/bytemd/src/toolbar.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
export let sidebar: false | 'help' | 'toc'
1818
export let locale: BytemdLocale
1919
export let actions: BytemdAction[]
20+
export let rightAfferentActions: BytemdAction[]
2021
2122
interface RightAction extends BytemdAction {
2223
active?: boolean
@@ -131,6 +132,7 @@
131132
},
132133
},
133134
},
135+
...rightAfferentActions
134136
] as RightAction[]
135137
136138
const tippyClass = 'bytemd-tippy'

packages/bytemd/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ export interface BytemdAction {
116116
* Action title
117117
*/
118118
title?: string
119+
/**
120+
* Action icon position
121+
*
122+
* Specifies that the action icon is in the toolbar position, which defaults to the left
123+
*/
124+
position?: 'left' | 'right'
119125
/**
120126
* Action icon (16x16), usually inline svg
121127
*/

0 commit comments

Comments
 (0)