refactor: flatten locale map

This commit is contained in:
Rongjian Zhang 2021-07-03 11:26:39 +08:00
parent f4567b8ebe
commit 61279f6d00
17 changed files with 382 additions and 476 deletions

View File

@ -31,7 +31,6 @@
"@types/lodash.throttle": "^4.1.6",
"@types/mdast": "^3.0.3",
"codemirror-ssr": "^0.0.6",
"deepmerge": "^4.2.2",
"hast-util-sanitize": "^3.0.2",
"lodash.debounce": "^4.0.8",
"lodash.throttle": "^4.1.1",
@ -42,7 +41,6 @@
"remark-rehype": "^8.1.0",
"select-files": "^1.0.1",
"tippy.js": "^6.3.1",
"tsdef": "^0.0.14",
"unified": "^9.2.1",
"unist-util-visit": "^2.0.3",
"vfile": "^4.2.1",

View File

@ -4,12 +4,7 @@
import type { Editor, KeyMap, Linter, Annotation } from 'codemirror'
import type { Root, Element } from 'hast'
import type { VFile } from 'vfile'
import type {
BytemdEditorContext,
BytemdLocale,
BytemdPlugin,
EditorProps,
} from './types'
import type { BytemdEditorContext, BytemdPlugin, EditorProps } from './types'
import { onMount, createEventDispatcher, onDestroy, tick } from 'svelte'
import debounce from 'lodash.debounce'
import throttle from 'lodash.throttle'
@ -25,7 +20,6 @@
import Status from './status.svelte'
import { icons } from './icons'
import en from './locales/en.json'
import deepmerge from 'deepmerge'
import Help from './help.svelte'
import factory from 'codemirror-ssr'
import usePlaceholder from 'codemirror-ssr/addon/display/placeholder'
@ -51,9 +45,7 @@
export let overridePreview: EditorProps['overridePreview']
export let maxLength: EditorProps['maxLength']
// do deep merge to support incomplete locales, use en as fallback
$: mergedLocale = deepmerge(en, locale ?? {}) as BytemdLocale
const mergedLocale = { ...en, ...locale }
const dispatch = createEventDispatcher()
$: actions = getBuiltinActions(mergedLocale, plugins, uploadImages)

View File

@ -151,11 +151,11 @@ export function getBuiltinActions(
handler: {
type: 'dropdown',
actions: [1, 2, 3, 4, 5, 6].map((level) => ({
title: locale.action[`h${level}` as keyof typeof locale.action],
title: locale[`h${level}` as keyof BytemdLocale],
icon: icons[`h${level}` as keyof typeof icons],
cheatsheet:
level <= 3
? `${'#'.repeat(level)} ${locale.action.headingText}`
? `${'#'.repeat(level)} ${locale.headingText}`
: undefined,
handler: {
type: 'action',
@ -172,9 +172,9 @@ export function getBuiltinActions(
},
},
{
title: locale.action.bold,
title: locale.bold,
icon: icons.bold,
cheatsheet: `**${locale.action.boldText}**`,
cheatsheet: `**${locale.boldText}**`,
handler: {
type: 'action',
shortcut: getShortcutWithPrefix('B'),
@ -185,9 +185,9 @@ export function getBuiltinActions(
},
},
{
title: locale.action.italic,
title: locale.italic,
icon: icons.italic,
cheatsheet: `*${locale.action.italicText}*`,
cheatsheet: `*${locale.italicText}*`,
handler: {
type: 'action',
shortcut: getShortcutWithPrefix('I'),
@ -198,9 +198,9 @@ export function getBuiltinActions(
},
},
{
title: locale.action.quote,
title: locale.quote,
icon: icons.quote,
cheatsheet: `> ${locale.action.quotedText}`,
cheatsheet: `> ${locale.quotedText}`,
handler: {
type: 'action',
click({ replaceLines, editor }) {
@ -210,9 +210,9 @@ export function getBuiltinActions(
},
},
{
title: locale.action.link,
title: locale.link,
icon: icons.link,
cheatsheet: `[${locale.action.linkText}](url)`,
cheatsheet: `[${locale.linkText}](url)`,
handler: {
type: 'action',
shortcut: getShortcutWithPrefix('K'),
@ -228,9 +228,9 @@ export function getBuiltinActions(
},
},
{
title: locale.action.image,
title: locale.image,
icon: icons.image,
cheatsheet: `![${locale.action.imageAlt}](url "${locale.action.imageTitle}")`,
cheatsheet: `![${locale.imageAlt}](url "${locale.imageTitle}")`,
handler: uploadImages
? {
type: 'action',
@ -249,9 +249,9 @@ export function getBuiltinActions(
: undefined,
},
{
title: locale.action.code,
title: locale.code,
icon: icons.code,
cheatsheet: '`' + locale.action.codeText + '`',
cheatsheet: '`' + locale.codeText + '`',
handler: {
type: 'action',
shortcut: getShortcutWithPrefix('K', true),
@ -262,9 +262,9 @@ export function getBuiltinActions(
},
},
{
title: locale.action.codeBlock,
title: locale.codeBlock,
icon: icons.codeBlock,
cheatsheet: '```' + locale.action.codeLang + '↵',
cheatsheet: '```' + locale.codeLang + '↵',
handler: {
type: 'action',
shortcut: getShortcutWithPrefix('C', true),
@ -279,9 +279,9 @@ export function getBuiltinActions(
},
},
{
title: locale.action.ul,
title: locale.ul,
icon: icons.ul,
cheatsheet: `- ${locale.action.ulItem}`,
cheatsheet: `- ${locale.ulItem}`,
handler: {
type: 'action',
shortcut: getShortcutWithPrefix('U', true),
@ -292,9 +292,9 @@ export function getBuiltinActions(
},
},
{
title: locale.action.ol,
title: locale.ol,
icon: icons.ol,
cheatsheet: `1. ${locale.action.olItem}`,
cheatsheet: `1. ${locale.olItem}`,
handler: {
type: 'action',
shortcut: getShortcutWithPrefix('O', true),
@ -305,7 +305,7 @@ export function getBuiltinActions(
},
},
{
title: locale.action.hr,
title: locale.hr,
icon: icons.hr,
cheatsheet: '---',
},

View File

@ -25,7 +25,7 @@
</script>
<div class="bytemd-help" class:bytemd-hidden={!visible}>
<h2>{locale.sidebar.cheatsheet}</h2>
<h2>{locale.cheatsheet}</h2>
<ul>
{#each items as action}
{#if action.cheatsheet}
@ -39,7 +39,7 @@
{/if}
{/each}
</ul>
<h2>{locale.sidebar.shortcuts}</h2>
<h2>{locale.shortcuts}</h2>
<ul>
{#each items as action}
{#if action.handler && action.handler.type === 'action' && action.handler.shortcut}

View File

@ -1,5 +1,4 @@
{
"action": {
"quotedText": "text citat",
"quote": "Cita",
"linkText": "text de lenllaç",
@ -26,16 +25,11 @@
"h4": "Encapçalament 4",
"h3": "Encapçalament 3",
"h2": "Encapçalament 2",
"h1": "Encapçalament 1"
},
"sidebar": {
"h1": "Encapçalament 1",
"toc": "Taula de contingut",
"cheatsheet": "Referència ràpida del Markdown",
"shortcuts": "Dreceres"
},
"toolbar": {
"shortcuts": "Dreceres",
"closeToc": "Tanca la taula de contingut",
"toc": "Taula de contingut",
"exitFullscreen": "Surt de la pantalla completa",
"fullscreen": "Pantalla completa",
"source": "Codi font",
@ -46,12 +40,9 @@
"exitWriteOnly": "Surt de «Només escriptura»",
"writeOnly": "Només escriptura",
"preview": "Previsualització",
"write": "Escriptura"
},
"status": {
"write": "Escriptura",
"top": "Desplaçat fins al capdamunt",
"sync": "Desplaçament síncron",
"lines": "Línies",
"words": "Mots"
}
}

View File

@ -1,5 +1,4 @@
{
"toolbar": {
"write": "Write",
"preview": "Preview",
"writeOnly": "Write only",
@ -12,21 +11,14 @@
"closeToc": "Close table of contents",
"fullscreen": "Fullscreen",
"exitFullscreen": "Exit fullscreen",
"source": "Source code"
},
"sidebar": {
"toc": "Table of contents",
"source": "Source code",
"cheatsheet": "Markdown Cheatsheet",
"shortcuts": "Shortcuts"
},
"status": {
"shortcuts": "Shortcuts",
"words": "Words",
"lines": "Lines",
"sync": "Scroll sync",
"top": "Scroll to top",
"limited": "The maximum character limit has been reached"
},
"action": {
"limited": "The maximum character limit has been reached",
"h1": "Heading 1",
"h2": "Heading 2",
"h3": "Heading 3",
@ -55,4 +47,3 @@
"olItem": "item",
"hr": "Horizontal rule"
}
}

View File

@ -1,5 +1,4 @@
{
"toolbar": {
"write": "Escribir",
"source": "Código fuente",
"exitFullscreen": "Salir de pantalla completa",
@ -12,9 +11,7 @@
"previewOnly": "Solo previsualización",
"writeOnly": "Solo escritura",
"exitPreviewOnly": "Salir de Solo previsualización",
"exitWriteOnly": "Salir de Solo escritura"
},
"action": {
"exitWriteOnly": "Salir de Solo escritura",
"hr": "Línea horizontal",
"linkText": "texto de enlace",
"link": "Enlace",
@ -41,17 +38,11 @@
"ul": "Lista con bolos",
"codeLang": "lenguaje",
"imageTitle": "título",
"imageAlt": "texto alt."
},
"sidebar": {
"imageAlt": "texto alt.",
"cheatsheet": "Referencia rápida de Markdown",
"shortcuts": "Atajos",
"toc": "Sumario"
},
"status": {
"sync": "Desplazamiento síncrono",
"lines": "Renglones",
"words": "Palabras",
"top": "Desplazarse hasta arriba"
}
}

View File

@ -1,31 +1,23 @@
{
"sidebar": {
"cheatsheet": "マークダウン カンニングペーパー",
"shortcuts": "ショートカット",
"toc": "目次"
},
"status": {
"toc": "目次",
"lines": "線",
"top": "トップにスクロールします",
"sync": "スクロール同期",
"words": "言葉"
},
"toolbar": {
"words": "言葉",
"preview": "プレビュー",
"exitPreviewOnly": "プレビューのみを終了",
"source": "原始コード",
"exitFullscreen": "全画面を閉じます",
"fullscreen": "全画面",
"closeToc": "目次を閉じます",
"toc": "目次",
"help": "ヘルプビューア",
"closeHelp": "ヘルプビューアを閉じます",
"previewOnly": "試写専用",
"exitWriteOnly": "Exit 書き込み専用",
"writeOnly": "書き込み専用",
"write": "書く"
},
"action": {
"write": "書く",
"hr": "区切り線",
"olItem": "アイテム",
"ol": "番号付きリスト",
@ -54,4 +46,3 @@
"h2": "見出し2",
"h1": "見出し1"
}
}

View File

@ -1,5 +1,4 @@
{
"toolbar": {
"exitPreviewOnly": "미리보기 전용 종료",
"previewOnly": "미리보기 전용",
"exitWriteOnly": "쓰기 전용 종료",
@ -10,14 +9,9 @@
"closeHelp": "도움말 닫기",
"help": "도움말",
"preview": "미리보기",
"write": "작성"
},
"action": {
"write": "작성",
"image": "이미지",
"link": "링크"
},
"status": {
"link": "링크",
"top": "맨 위로 스크롤",
"sync": "스크롤 동기화"
}
}

View File

@ -1,5 +1,4 @@
{
"action": {
"ol": "Нумерованный список",
"codeLang": "язык",
"codeBlock": "Блок кода",
@ -24,15 +23,11 @@
"olItem": "элемент",
"ulItem": "элемент",
"imageTitle": "название",
"ul": "Маркированный список"
},
"status": {
"ul": "Маркированный список",
"sync": "Синхронизация прокрутки",
"words": "Слова",
"top": "Пролистать наверх",
"lines": "Строки"
},
"toolbar": {
"lines": "Строки",
"source": "Исходный код",
"fullscreen": "Полноэкранный режим",
"exitFullscreen": "Выйти из полноэкранного режима",
@ -44,11 +39,7 @@
"preview": "Предпросмотр",
"exitPreviewOnly": "Выйти из режима предпросмотра",
"writeOnly": "Только запись",
"write": "Записать"
},
"sidebar": {
"write": "Записать",
"shortcuts": "Горячие клавиши",
"cheatsheet": "Шпаргалка по Markdown",
"toc": "Оглавление"
}
"cheatsheet": "Шпаргалка по Markdown"
}

View File

@ -1,5 +1,4 @@
{
"toolbar": {
"preview": "预览",
"write": "编辑",
"source": "源代码",
@ -12,9 +11,7 @@
"exitPreviewOnly": "恢复默认",
"previewOnly": "仅预览区",
"exitWriteOnly": "恢复默认",
"writeOnly": "仅编辑区"
},
"action": {
"writeOnly": "仅编辑区",
"hr": "分割线",
"olItem": "项目",
"ol": "有序列表",
@ -41,18 +38,12 @@
"h4": "四级标题",
"h3": "三级标题",
"h2": "二级标题",
"h1": "一级标题"
},
"status": {
"h1": "一级标题",
"top": "回到顶部",
"sync": "同步滚动",
"lines": "行数",
"words": "字数",
"limited": "已达最大字符数限制"
},
"sidebar": {
"limited": "已达最大字符数限制",
"shortcuts": "快捷键",
"cheatsheet": "Markdown 语法",
"toc": "目录"
}
"cheatsheet": "Markdown 语法"
}

View File

@ -1,5 +1,4 @@
{
"action": {
"imageAlt": "alt",
"hr": "分割線",
"olItem": "項目",
@ -26,25 +25,18 @@
"h4": "四級標題",
"h3": "三級標題",
"h2": "二級標題",
"h1": "一級標題"
},
"status": {
"h1": "一級標題",
"top": "回到頂部",
"sync": "同步滾動",
"lines": "行數",
"words": "字數"
},
"sidebar": {
"words": "字數",
"shortcuts": "快捷鍵",
"cheatsheet": "Markdown 語法",
"toc": "目錄"
},
"toolbar": {
"toc": "目錄",
"source": "源碼",
"exitFullscreen": "退出全屏",
"fullscreen": "全屏",
"closeToc": "關閉目錄",
"toc": "目錄",
"closeHelp": "關閉幫助",
"help": "幫助",
"exitPreviewOnly": "恢復默認",
@ -54,4 +46,3 @@
"preview": "預覽",
"write": "編輯"
}
}

View File

@ -18,13 +18,13 @@
<div class="bytemd-status">
<div class="bytemd-status-left">
<span>
{locale.status.words}: <strong>{words}</strong>
{locale.words}: <strong>{words}</strong>
</span>
<span>
{locale.status.lines}: <strong>{lines}</strong>
{locale.lines}: <strong>{lines}</strong>
</span>
{#if islimited}
<span class="danger">{locale.status.limited}</span>
<span class="danger">{locale.limited}</span>
{/if}
</div>
@ -36,9 +36,9 @@
checked={syncEnabled}
on:change={() => dispatch('sync', !syncEnabled)}
/>
{locale.status.sync}
{locale.sync}
</label>
{/if}
<span on:click={() => dispatch('top')}>{locale.status.top}</span>
<span on:click={() => dispatch('top')}>{locale.top}</span>
</div>
</div>

View File

@ -53,7 +53,7 @@
</script>
<div class="bytemd-toc" class:bytemd-hidden={!visible}>
<h2>{locale.sidebar.toc}</h2>
<h2>{locale.toc}</h2>
<ul>
{#each items as item, index}
<li

View File

@ -66,7 +66,7 @@
// },
// },
{
title: tocActive ? locale.toolbar.closeToc : locale.toolbar.toc,
title: tocActive ? locale.closeToc : locale.toc,
icon: icons.toc,
handler: {
type: 'action',
@ -77,7 +77,7 @@
active: tocActive,
},
{
title: helpActive ? locale.toolbar.closeHelp : locale.toolbar.help,
title: helpActive ? locale.closeHelp : locale.help,
icon: icons.help,
handler: {
type: 'action',
@ -88,9 +88,7 @@
active: helpActive,
},
{
title: writeActive
? locale.toolbar.exitWriteOnly
: locale.toolbar.writeOnly,
title: writeActive ? locale.exitWriteOnly : locale.writeOnly,
icon: icons.left,
handler: {
type: 'action',
@ -102,9 +100,7 @@
hidden: !split,
},
{
title: previewActive
? locale.toolbar.exitPreviewOnly
: locale.toolbar.previewOnly,
title: previewActive ? locale.exitPreviewOnly : locale.previewOnly,
icon: icons.right,
handler: {
type: 'action',
@ -116,9 +112,7 @@
hidden: !split,
},
{
title: fullscreen
? locale.toolbar.exitFullscreen
: locale.toolbar.fullscreen,
title: fullscreen ? locale.exitFullscreen : locale.fullscreen,
icon: fullscreen ? icons.fullscreenOff : icons.fullscreenOn,
handler: {
type: 'action',
@ -128,7 +122,7 @@
},
},
{
title: locale.toolbar.source,
title: locale.source,
icon: icons.source,
handler: {
type: 'action',
@ -299,14 +293,14 @@
class="bytemd-toolbar-tab"
class:bytemd-toolbar-tab-active={activeTab !== 'preview'}
>
{locale.toolbar.write}
{locale.write}
</div>
<div
on:click={() => dispatch('tab', 'preview')}
class="bytemd-toolbar-tab"
class:bytemd-toolbar-tab-active={activeTab === 'preview'}
>
{locale.toolbar.preview}
{locale.preview}
</div>
<!-- <div class={['bytemd-toolbar-icon', tippyClass].join(' ')}>
{@html icons.more}

View File

@ -6,7 +6,6 @@ import type CodeMirror from 'codemirror'
import type { EditorUtils } from './editor'
import type en from './locales/en.json'
import type { Image } from 'mdast'
import type { DeepPartial } from 'tsdef'
export type BytemdLocale = typeof en
@ -144,7 +143,7 @@ export interface EditorProps extends ViewerProps {
*
* @defaultValue en
*/
locale?: DeepPartial<BytemdLocale>
locale?: Partial<BytemdLocale>
/**
* Handle images upload
*/

View File

@ -208,8 +208,8 @@ importers:
'@bytemd/plugin-mermaid': link:../../packages/plugin-mermaid
bytemd: link:../../packages/bytemd
devDependencies:
'@sveltejs/adapter-node': 1.0.0-next.29
'@sveltejs/kit': 1.0.0-next.119_rollup@2.52.7+svelte@3.38.2
'@sveltejs/adapter-node': 1.0.0-next.30
'@sveltejs/kit': 1.0.0-next.120_rollup@2.52.7+svelte@3.38.2
sass: 1.32.12
svelte: 3.38.2
svelte-preprocess: 4.7.3_0f957d27398e96725816240c3a3a5bfb
@ -307,7 +307,6 @@ importers:
'@types/lodash.throttle': ^4.1.6
'@types/mdast': ^3.0.3
codemirror-ssr: ^0.0.6
deepmerge: ^4.2.2
hast-util-sanitize: ^3.0.2
lodash.debounce: ^4.0.8
lodash.throttle: ^4.1.1
@ -318,7 +317,6 @@ importers:
remark-rehype: ^8.1.0
select-files: ^1.0.1
tippy.js: ^6.3.1
tsdef: ^0.0.14
unified: ^9.2.1
unist-util-visit: ^2.0.3
vfile: ^4.2.1
@ -330,7 +328,6 @@ importers:
'@types/lodash.throttle': 4.1.6
'@types/mdast': 3.0.3
codemirror-ssr: 0.0.6
deepmerge: 4.2.2
hast-util-sanitize: 3.0.2
lodash.debounce: 4.0.8
lodash.throttle: 4.1.1
@ -341,7 +338,6 @@ importers:
remark-rehype: 8.1.0
select-files: 1.0.1
tippy.js: 6.3.1
tsdef: 0.0.14
unified: 9.2.1
unist-util-visit: 2.0.3
vfile: 4.2.1
@ -4479,15 +4475,15 @@ packages:
'@sinonjs/commons': 1.8.3
dev: true
/@sveltejs/adapter-node/1.0.0-next.29:
resolution: {integrity: sha512-F773SXfSUGZVtwVvi495uXK71oAZlLF1ZvnLi/R2bcCoDN5y8RA7fSWfrMH78EyM9dLOB5RSdShv8PcE5tpMUg==}
/@sveltejs/adapter-node/1.0.0-next.30:
resolution: {integrity: sha512-DTvazMWs0h5zG++uaT7Yg0JkWfqU8htu3EYFsKh+oeDMviJ8SRXTXSZFtBGr2ltyPBwKUvgEoUYITOl1NHimKQ==}
dependencies:
esbuild: 0.12.12
tiny-glob: 0.2.9
dev: true
/@sveltejs/kit/1.0.0-next.119_rollup@2.52.7+svelte@3.38.2:
resolution: {integrity: sha512-Pp971N1m2yKQg01kUxwQAolhUb1y9NNPV7zfQLIHQdvlFwrNI5hvOMlGiHj26mM5AyQSU4pnH8X1YUPSqi57gw==}
/@sveltejs/kit/1.0.0-next.120_rollup@2.52.7+svelte@3.38.2:
resolution: {integrity: sha512-1sVhlF9zea9Er6abAjqRefQ1mIE9AjtYdRek3ilUTQ44FM2w/e8VgZCo5XXbnz9M564OaZnihyS9XjINlu4Chg==}
engines: {node: ^12.20 || >=14.13}
hasBin: true
peerDependencies:
@ -16654,10 +16650,6 @@ packages:
typescript: 4.3.5
dev: false
/tsdef/0.0.14:
resolution: {integrity: sha512-UjMD4XKRWWFlFBfwKVQmGFT5YzW/ZaF8x6KpCDf92u9wgKeha/go3FU0e5WqDjXsCOdfiavCkfwfVHNDxRDGMA==}
dev: false
/tslib/1.14.1:
resolution: {integrity: sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==}