Skip to content

Commit

Permalink
Add ability to export json
Browse files Browse the repository at this point in the history
  • Loading branch information
yezhiyi9670 committed Jul 23, 2023
1 parent 2c0bd1a commit 6e767af
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/renderer/appbar/appbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const useStyles = createUseStyles({

interface AppBarProps {
onItemClick: (key: string) => void
onItemRightClick: (key: string) => void
}
export function AppBar(props: AppBarProps) {
const classes = useStyles()
Expand All @@ -36,6 +37,7 @@ export function AppBar(props: AppBarProps) {
itemKey={entry.key}
icon={entry.icon}
onClick={() => {props.onItemClick(entry.key)}}
onRightClick={() => {props.onItemRightClick(entry.key)}}
/>
}

Expand Down
3 changes: 2 additions & 1 deletion src/renderer/appbar/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ interface AppBarButtonProps {
itemKey: string
icon: React.ReactNode
onClick?: () => void
onRightClick?: () => void
}
export function AppBarButton(props: AppBarButtonProps) {
const classes = useStyles()
const LNG = useI18n()

return <button onClick={props.onClick} type='button' className={classes.item}>
return <button onClick={props.onClick} onContextMenu={props.onRightClick} type='button' className={classes.item}>
<div className={classes.iconContainer}>
{props.icon}
</div>
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/dialog/hint/entries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ const entries: HintEntries = [
confirm: true,
focusButton: true
},
{
key: 'jsonUsage',
lines: 2,
prefKey: 'hintJsonUsage',
dismiss: false,
confirm: true,
focusButton: true
},
{
key: 'printEssence',
lines: 3,
Expand Down
18 changes: 16 additions & 2 deletions src/renderer/i18n/lang/zh_cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ export default flattenI18nData({
"toast": {
"open_fail": "无法打开文件 ${0}",
"save_fail": "无法保存至 ${0}",
"export_fail": "无法导出到 ${0}",
"save_before_export": "不能导出未保存过的文件。",
"exported": "已将预览导出到 ${0}",
"preview_exported": "已将预览导出到 ${0}",
"preview_export_fail": "无法导出到 ${0}",
"json_exported": "已将数据导出到 ${0}",
"json_export_fail": "无法导出到 ${0}",
"print_fail": "准备打印环境失败",
"print_launch_fail": "无法启动浏览器,请检查设置中的浏览器路径是否正确。",
"new_version": "新版本 ${0} 现已可用,可前往官网下载。",
Expand Down Expand Up @@ -78,6 +80,14 @@ export default flattenI18nData({
},
"confirm": "导出"
},
"jsonUsage": {
"title": "JSON 用途提示",
"line": {
"1": `导出的 JSON 文件包含解析后的乐谱数据。这些数据可供第三方软件使用,可以用来渲染乐谱,也可以用来生成一些音乐相关的内容,例如 MIDI 文件。`,
"2": `JSON 文件是一种目标格式,并不直接包含源代码。`,
},
"confirm": "导出"
},
"printEssence": {
"title": "如何打印",
"line": {
Expand Down Expand Up @@ -258,6 +268,10 @@ export default flattenI18nData({
"title": "HTML 用途提示",
"desc": "触发:导出 HTML 文件前。"
},
"hintJsonUsage": {
"title": "JSON 用途提示",
"desc": "触发:导出 JSON 文件前。JSON 文件导出可通过右键点击“导出”按钮触发。"
},
"hintExportEssence": {
"title": "关于 PNG/PDF 导出的本质",
"desc": "触发:导出 PNG/PDF 前。"
Expand Down
37 changes: 34 additions & 3 deletions src/renderer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ function AppIn() {
const exportContent = editorApi.exportHtml(exportTemplate, localFontLocation)
const basename = window.Path.basename(path)
if(await window.FileSystem.saveText(path, exportContent)) {
showToast(LNG('toast.exported', basename))
showToast(LNG('toast.preview_exported', basename))
} else {
showToast(LNG('toast.export_fail', basename))
showToast(LNG('toast.preview_export_fail', basename))
}
}
function exportHtml() {
Expand All @@ -229,6 +229,32 @@ function AppIn() {
})
})
}
function exportJson() {
if(!checkExportCooldown()) {
return
}
callRef(editorApiRef, api => {
callRef(hintApiRef, async hint => {
const filename = api.getFilename()
if(filename === undefined) {
showToast(LNG('toast.save_before_export'))
return
}
if(!await hint.invoke('jsonUsage')) {
return
}
const parsedFn = window.Path.parse(filename)
const exportPath = window.Path.join(parsedFn.dir, parsedFn.name + '.json')
const exportContent = api.exportJson()
const basename = window.Path.basename(exportPath)
if(await window.FileSystem.saveText(exportPath, exportContent)) {
showToast(LNG('toast.json_exported', basename))
} else {
showToast(LNG('toast.json_export_fail', basename))
}
})
})
}
function printHtml() {
if(!checkExportCooldown()) {
return
Expand Down Expand Up @@ -292,6 +318,11 @@ function AppIn() {
printHtml()
}
}
async function handleAppBarRightItem(key: string) {
if(key == 'export') {
exportJson()
}
}
async function openDocumentPath(path: string) {
callRef(editorApiRef, async api => {
const data = await window.FileSystem.openText(path)
Expand Down Expand Up @@ -422,7 +453,7 @@ function AppIn() {
}
`}</style>
<div className={classes.appbar}>
<AppBar onItemClick={handleAppBarItem} />
<AppBar onItemClick={handleAppBarItem} onItemRightClick={handleAppBarRightItem} />
s</div>
<div className={classes.content}>
<IntegratedEditor
Expand Down
22 changes: 18 additions & 4 deletions src/renderer/nmn/integrated-editor/IntegratedEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,15 @@ export const __IntegratedEditor = React.forwardRef<IntegratedEditorApi, __Props>
})
const updateResult = useMethod((valueOverride?: string) => {
if(undefined === valueOverride && !isPreviewDirty) {
return
return parseResult.result
}
const parsed = parseNMN(valueOverride ?? value)
setParseResult({
result: parsed.result,
timing: parsed.time
})
setIsPreviewDirty(false)
return parsed.result
})

function handleChange(newValue: string) {
Expand Down Expand Up @@ -586,11 +587,12 @@ export const __IntegratedEditor = React.forwardRef<IntegratedEditorApi, __Props>
resetSession()
})
const handleExport = useMethod((template: string, localFontLocation: string) => {
let result = parseResult.result
if(isPreviewDirty) {
updateResult()
result = updateResult()
}
const fields = SparksNMN.render(parseResult.result.result, languageArray)
const pagedFields = SparksNMN.paginize(parseResult.result.result, fields, languageArray).result
const fields = SparksNMN.render(result.result, languageArray)
const pagedFields = SparksNMN.paginize(result.result, fields, languageArray).result
let title = NMNI18n.editorText(language, 'preview.new_title')
if(filename !== undefined) {
title = basenameName(filename)
Expand All @@ -606,6 +608,14 @@ export const __IntegratedEditor = React.forwardRef<IntegratedEditorApi, __Props>
))
return htmlData
})
const handleExportJson = useMethod(() => {
let result = parseResult.result
if(isPreviewDirty) {
result = updateResult()
}
const jsonResult = JSON.stringify(result)
return jsonResult
})
useImperativeHandle(ref, () => ({
getValue: () => {
return getValue()
Expand All @@ -630,6 +640,9 @@ export const __IntegratedEditor = React.forwardRef<IntegratedEditorApi, __Props>
},
exportHtml: (template: string, localFontLocation: string) => {
return handleExport(template, localFontLocation)
},
exportJson: () => {
return handleExportJson()
}
}))

Expand All @@ -645,4 +658,5 @@ export interface IntegratedEditorApi {
triggerNew: () => void
triggerOpen: (data: {path: string, content: string}) => void
exportHtml: (template: string, localFontLocation: string) => string
exportJson: () => string
}
7 changes: 6 additions & 1 deletion src/util/prefs/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const entries: PrefRendererInfo = [
entries: [
{
key: 'fontFamily',
defaultValue: ['string', "'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', 'Microsoft YaHei UI'"],
defaultValue: ['string', "'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', system-ui"],
type: 'string'
},
{
Expand Down Expand Up @@ -133,6 +133,11 @@ const entries: PrefRendererInfo = [
defaultValue: ['boolean', true],
type: 'boolean'
},
{
key: 'hintJsonUsage',
defaultValue: ['boolean', true],
type: 'boolean'
},
// {
// key: 'hintExportEssence',
// defaultValue: ['boolean', true],
Expand Down
4 changes: 2 additions & 2 deletions version-number.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"app": "1.14.16",
"core": "1.14.16"
"app": "1.14.17",
"core": "1.14.17"
}

0 comments on commit 6e767af

Please sign in to comment.