From 3b43957cee0eef05ccf9f04c1147577ed2db77a4 Mon Sep 17 00:00:00 2001 From: Xmader Date: Tue, 24 Nov 2020 04:36:01 -0500 Subject: [PATCH] feat: load webmscore in nodejs --- src/mscore.ts | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/mscore.ts b/src/mscore.ts index 9e0be1a..e9efcfa 100644 --- a/src/mscore.ts +++ b/src/mscore.ts @@ -1,6 +1,8 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ import { fetchMscz } from './mscz' import { fetchData } from './utils' +import { ScoreInfo } from './scoreinfo' const WEBMSCORE_URL = 'https://cdn.jsdelivr.net/npm/webmscore@0.10/webmscore.js' @@ -12,25 +14,41 @@ const SF3_URL = 'https://cdn.jsdelivr.net/npm/@librescore/sf3/FluidR3Mono_GM.sf3 const SOUND_FONT_LOADED = Symbol('SoundFont loaded') export type WebMscore = import('webmscore').default +export type WebMscoreConstr = typeof import('webmscore').default -const initMscore = async (w: Window) => { - if (!w['WebMscore']) { - // init webmscore (https://github.com/LibreScore/webmscore) - const script = w.document.createElement('script') - script.src = WEBMSCORE_URL - w.document.body.append(script) - await new Promise(resolve => { script.onload = resolve }) +const initMscore = async (w?: Window): Promise => { + if (w !== undefined) { // attached to a page + if (!w['WebMscore']) { + // init webmscore (https://github.com/LibreScore/webmscore) + const script = w.document.createElement('script') + script.src = WEBMSCORE_URL + w.document.body.append(script) + await new Promise(resolve => { script.onload = resolve }) + } + return w['WebMscore'] as WebMscoreConstr + } else { // nodejs + return require('webmscore').default as WebMscoreConstr } } let fonts: Promise | undefined -const initFonts = () => { +const initFonts = (nodeJs: boolean) => { // load CJK fonts // CJK (East Asian) characters will be rendered as "tofu" if there is no font if (!fonts) { - fonts = Promise.all( - FONT_URLS.map(url => fetchData(url)), - ) + if (nodeJs) { + // module.exports.CN = ..., module.exports.KR = ... + const FONTS = Object.values(require('@librescore/fonts')) + + const fs = require('fs') + fonts = Promise.all( + FONTS.map((path: string) => fs.promises.readFile(path) as Promise), + ) + } else { + fonts = Promise.all( + FONT_URLS.map(url => fetchData(url)), + ) + } } } @@ -46,14 +64,13 @@ export const loadSoundFont = (score: WebMscore): Promise => { return score[SOUND_FONT_LOADED] as Promise } -export const loadMscore = async (w: Window): Promise => { - initFonts() - await initMscore(w) +export const loadMscore = async (scoreinfo: ScoreInfo, w?: Window): Promise => { + initFonts(w === undefined) + const WebMscore = await initMscore(w) - const WebMscore: typeof import('webmscore').default = w['WebMscore'] // parse mscz data const data = new Uint8Array( - new Uint8Array(await fetchMscz()), // copy its ArrayBuffer + new Uint8Array(await fetchMscz(scoreinfo)), // copy its ArrayBuffer ) const score = await WebMscore.load('mscz', data, await fonts) await score.generateExcerpts()