Skip to content

Commit 2b12533

Browse files
committed
feat: add code button
1 parent 1bc451d commit 2b12533

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

packages/bytemd/src/icons.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ export const iconMap = {
1919
'<svg viewBox="0 0 16 16"><path fill-rule="evenodd" d="M2.5 2.75a.25.25 0 01.25-.25h10.5a.25.25 0 01.25.25v10.5a.25.25 0 01-.25.25H2.75a.25.25 0 01-.25-.25V2.75zM2.75 1A1.75 1.75 0 001 2.75v10.5c0 .966.784 1.75 1.75 1.75h10.5A1.75 1.75 0 0015 13.25V2.75A1.75 1.75 0 0013.25 1H2.75zm9.03 5.28a.75.75 0 00-1.06-1.06L6.75 9.19 5.28 7.72a.75.75 0 00-1.06 1.06l2 2a.75.75 0 001.06 0l4.5-4.5z"/></svg>',
2020
info:
2121
'<svg viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8 1.5a6.5 6.5 0 100 13 6.5 6.5 0 000-13zM0 8a8 8 0 1116 0A8 8 0 010 8zm6.5-.25A.75.75 0 017.25 7h1a.75.75 0 01.75.75v2.75h.25a.75.75 0 010 1.5h-2a.75.75 0 010-1.5h.25v-2h-.25a.75.75 0 01-.75-.75zM8 6a1 1 0 100-2 1 1 0 000 2z"/></svg>',
22+
code:
23+
'<svg viewBox="0 0 16 16"><path fill-rule="evenodd" d="M4.72 3.22a.75.75 0 011.06 1.06L2.06 8l3.72 3.72a.75.75 0 11-1.06 1.06L.47 8.53a.75.75 0 010-1.06l4.25-4.25zm6.56 0a.75.75 0 10-1.06 1.06L13.94 8l-3.72 3.72a.75.75 0 101.06 1.06l4.25-4.25a.75.75 0 000-1.06l-4.25-4.25z"/></svg>'
2224
};

packages/bytemd/src/toolbar.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,31 @@ const leftItems: BytemdToolbarItem[] = [
5353
cm.focus();
5454
},
5555
},
56+
{
57+
tooltip: 'code',
58+
iconHtml: iconMap.code,
59+
onClick(cm) {
60+
const [selection] = cm.listSelections();
61+
const lines = cm
62+
.getRange(
63+
{ line: selection.anchor.line, ch: 0 },
64+
// @ts-ignore
65+
{ line: selection.head.line }
66+
)
67+
.split('\n');
68+
if (lines.length > 1) {
69+
cm.replaceRange(
70+
['```', ...lines, '```'].join('\n'),
71+
{ line: selection.anchor.line, ch: 0 },
72+
// @ts-ignore
73+
{ line: selection.head.line }
74+
);
75+
cm.focus();
76+
} else {
77+
handleText(cm, '`', '`');
78+
}
79+
},
80+
},
5681
{
5782
tooltip: 'table',
5883
iconHtml: iconMap.table,

scripts/icons.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const iconMap = {
1313
ul: { key: 'list-unordered' },
1414
tasklist: {},
1515
info: {},
16+
code: {},
1617
};
1718

1819
let code = 'export const iconMap = {';

0 commit comments

Comments
 (0)