refactor: detect nodejs
This commit is contained in:
parent
bfdd80a364
commit
2fbfbede86
3 changed files with 13 additions and 6 deletions
5
package-lock.json
generated
5
package-lock.json
generated
|
@ -525,6 +525,11 @@
|
|||
"minimalistic-assert": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"detect-node": {
|
||||
"version": "2.0.4",
|
||||
"resolved": "https://registry.npmjs.org/detect-node/-/detect-node-2.0.4.tgz",
|
||||
"integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw=="
|
||||
},
|
||||
"dfa": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/dfa/-/dfa-1.2.0.tgz",
|
||||
|
|
|
@ -36,6 +36,7 @@
|
|||
"dist/main.js"
|
||||
],
|
||||
"dependencies": {
|
||||
"detect-node": "^2.0.4",
|
||||
"pdfkit": "git+https://github.com/Xmader/pdfkit.git",
|
||||
"svg-to-pdfkit": "^0.1.8",
|
||||
"webmscore": "^0.10.4"
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
import { fetchMscz } from './mscz'
|
||||
import { fetchData } from './utils'
|
||||
import { ScoreInfo } from './scoreinfo'
|
||||
import isNodeJs from 'detect-node'
|
||||
|
||||
const WEBMSCORE_URL = 'https://cdn.jsdelivr.net/npm/webmscore@0.10/webmscore.js'
|
||||
|
||||
|
@ -16,8 +17,8 @@ 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): Promise<WebMscoreConstr> => {
|
||||
if (w !== undefined) { // attached to a page
|
||||
const initMscore = async (w: Window): Promise<WebMscoreConstr> => {
|
||||
if (!isNodeJs) { // attached to a page
|
||||
if (!w['WebMscore']) {
|
||||
// init webmscore (https://github.com/LibreScore/webmscore)
|
||||
const script = w.document.createElement('script')
|
||||
|
@ -32,11 +33,11 @@ const initMscore = async (w?: Window): Promise<WebMscoreConstr> => {
|
|||
}
|
||||
|
||||
let fonts: Promise<Uint8Array[]> | undefined
|
||||
const initFonts = (nodeJs: boolean) => {
|
||||
const initFonts = () => {
|
||||
// load CJK fonts
|
||||
// CJK (East Asian) characters will be rendered as "tofu" if there is no font
|
||||
if (!fonts) {
|
||||
if (nodeJs) {
|
||||
if (isNodeJs) {
|
||||
// module.exports.CN = ..., module.exports.KR = ...
|
||||
const FONTS = Object.values(require('@librescore/fonts'))
|
||||
|
||||
|
@ -65,8 +66,8 @@ export const loadSoundFont = (score: WebMscore): Promise<void> => {
|
|||
}
|
||||
|
||||
export const loadMscore = async (scoreinfo: ScoreInfo, w?: Window): Promise<WebMscore> => {
|
||||
initFonts(w === undefined)
|
||||
const WebMscore = await initMscore(w)
|
||||
initFonts()
|
||||
const WebMscore = await initMscore(w!)
|
||||
|
||||
// parse mscz data
|
||||
const data = new Uint8Array(
|
||||
|
|
Loading…
Reference in a new issue