mirror of
https://github.com/mii443/obsidian-typst.git
synced 2025-09-03 15:59:13 +00:00
fix mobile
This commit is contained in:
71
src/main.ts
71
src/main.ts
@ -49,25 +49,23 @@ export default class TypstPlugin extends Plugin {
|
|||||||
fs: any;
|
fs: any;
|
||||||
|
|
||||||
async onload() {
|
async onload() {
|
||||||
|
this.textEncoder = new TextEncoder()
|
||||||
|
await this.loadSettings()
|
||||||
this.compilerWorker = (new CompilerWorker() as Worker);
|
this.compilerWorker = (new CompilerWorker() as Worker);
|
||||||
if (!Platform.isMobileApp) {
|
if (!Platform.isMobileApp) {
|
||||||
this.compilerWorker.postMessage(true);
|
this.compilerWorker.postMessage(true);
|
||||||
this.fs = require("fs")
|
this.fs = require("fs")
|
||||||
|
let fonts = await Promise.all(
|
||||||
|
//@ts-expect-error
|
||||||
|
(await window.queryLocalFonts() as Array)
|
||||||
|
.filter((font: { family: string; name: string; }) => this.settings.font_families.contains(font.family.toLowerCase()))
|
||||||
|
.map(
|
||||||
|
async (font: { blob: () => Promise<Blob>; }) => await (await font.blob()).arrayBuffer()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
this.compilerWorker.postMessage(fonts, fonts)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.textEncoder = new TextEncoder()
|
|
||||||
await this.loadSettings()
|
|
||||||
|
|
||||||
let fonts = await Promise.all(
|
|
||||||
//@ts-expect-error
|
|
||||||
(await window.queryLocalFonts() as Array)
|
|
||||||
.filter((font: { family: string; name: string; }) => this.settings.font_families.contains(font.family.toLowerCase()))
|
|
||||||
.map(
|
|
||||||
async (font: { blob: () => Promise<Blob>; }) => await (await font.blob()).arrayBuffer()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
this.compilerWorker.postMessage(fonts, fonts)
|
|
||||||
|
|
||||||
// Setup cutom canvas
|
// Setup cutom canvas
|
||||||
TypstRenderElement.compile = (a, b, c, d, e) => this.processThenCompileTypst(a, b, c, d, e)
|
TypstRenderElement.compile = (a, b, c, d, e) => this.processThenCompileTypst(a, b, c, d, e)
|
||||||
if (customElements.get("typst-renderer") == undefined) {
|
if (customElements.get("typst-renderer") == undefined) {
|
||||||
@ -233,7 +231,7 @@ export default class TypstPlugin extends Plugin {
|
|||||||
const dpr = window.devicePixelRatio;
|
const dpr = window.devicePixelRatio;
|
||||||
// * (72 / 96)
|
// * (72 / 96)
|
||||||
const pxToPt = (px: number) => px.toString() + "pt"
|
const pxToPt = (px: number) => px.toString() + "pt"
|
||||||
const sizing = `#let (WIDTH, HEIGHT, SIZE, THEME) = (${display ? pxToPt(size) : "auto"}, ${!display ? pxToPt(size) : "auto"}, ${pxToPt(fontSize * dpr)}, "${document.body.getCssPropertyValue("color-scheme")}")`
|
const sizing = `#let (WIDTH, HEIGHT, SIZE, THEME) = (${display ? pxToPt(size) : "auto"}, ${!display ? pxToPt(size) : "auto"}, ${pxToPt(fontSize)}, "${document.body.getCssPropertyValue("color-scheme")}")`
|
||||||
return this.compileToTypst(
|
return this.compileToTypst(
|
||||||
path,
|
path,
|
||||||
`${sizing}\n${this.settings.preamable.shared}\n${source}`,
|
`${sizing}\n${this.settings.preamable.shared}\n${source}`,
|
||||||
@ -385,33 +383,36 @@ class TypstSettingTab extends PluginSettingTab {
|
|||||||
.addTextArea((c) => c.setValue(this.plugin.settings.preamable.math).onChange(async (value) => { this.plugin.settings.preamable.math = value; await this.plugin.saveSettings() }))
|
.addTextArea((c) => c.setValue(this.plugin.settings.preamable.math).onChange(async (value) => { this.plugin.settings.preamable.math = value; await this.plugin.saveSettings() }))
|
||||||
|
|
||||||
//Font family settings
|
//Font family settings
|
||||||
const fontSettings = containerEl.createDiv({ cls: "setting-item font-settings" })
|
if (!Platform.isMobileApp) {
|
||||||
fontSettings.createDiv({ text: "Fonts", cls: "setting-item-name" })
|
|
||||||
fontSettings.createDiv({ text: "Font family names that should be loaded for Typst from your system. Requires a reload on change.", cls: "setting-item-description" })
|
|
||||||
|
|
||||||
const addFontsDiv = fontSettings.createDiv({ cls: "add-fonts-div" })
|
const fontSettings = containerEl.createDiv({ cls: "setting-item font-settings" })
|
||||||
const fontsInput = addFontsDiv.createEl('input', { type: "text", placeholder: "Enter a font family", cls: "font-input", })
|
fontSettings.createDiv({ text: "Fonts", cls: "setting-item-name" })
|
||||||
const addFontBtn = addFontsDiv.createEl('button', { text: "Add" })
|
fontSettings.createDiv({ text: "Font family names that should be loaded for Typst from your system. Requires a reload on change.", cls: "setting-item-description" })
|
||||||
|
|
||||||
const fontTagsDiv = fontSettings.createDiv({ cls: "font-tags-div" })
|
const addFontsDiv = fontSettings.createDiv({ cls: "add-fonts-div" })
|
||||||
|
const fontsInput = addFontsDiv.createEl('input', { type: "text", placeholder: "Enter a font family", cls: "font-input", })
|
||||||
|
const addFontBtn = addFontsDiv.createEl('button', { text: "Add" })
|
||||||
|
|
||||||
const addFontTag = async () => {
|
const fontTagsDiv = fontSettings.createDiv({ cls: "font-tags-div" })
|
||||||
if (!this.plugin.settings.font_families.contains(fontsInput.value)) {
|
|
||||||
this.plugin.settings.font_families.push(fontsInput.value.toLowerCase())
|
const addFontTag = async () => {
|
||||||
await this.plugin.saveSettings()
|
if (!this.plugin.settings.font_families.contains(fontsInput.value)) {
|
||||||
|
this.plugin.settings.font_families.push(fontsInput.value.toLowerCase())
|
||||||
|
await this.plugin.saveSettings()
|
||||||
|
}
|
||||||
|
fontsInput.value = ''
|
||||||
|
this.renderFontTags(fontTagsDiv)
|
||||||
}
|
}
|
||||||
fontsInput.value = ''
|
|
||||||
|
fontsInput.addEventListener('keydown', async (ev) => {
|
||||||
|
if (ev.key == "Enter") {
|
||||||
|
addFontTag()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
addFontBtn.addEventListener('click', async () => addFontTag())
|
||||||
|
|
||||||
this.renderFontTags(fontTagsDiv)
|
this.renderFontTags(fontTagsDiv)
|
||||||
}
|
}
|
||||||
|
|
||||||
fontsInput.addEventListener('keydown', async (ev) => {
|
|
||||||
if (ev.key == "Enter") {
|
|
||||||
addFontTag()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
addFontBtn.addEventListener('click', async () => addFontTag())
|
|
||||||
|
|
||||||
this.renderFontTags(fontTagsDiv)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { optimize } from 'svgo';
|
//@ts-ignore
|
||||||
|
import { optimize } from 'svgo/dist/svgo.browser.js';
|
||||||
|
|
||||||
export default class TypstRenderElement extends HTMLElement {
|
export default class TypstRenderElement extends HTMLElement {
|
||||||
static compile: (path: string, source: string, size: number, display: boolean, fontSize: number) => Promise<ImageData | string>;
|
static compile: (path: string, source: string, size: number, display: boolean, fontSize: number) => Promise<ImageData | string>;
|
||||||
|
Reference in New Issue
Block a user