Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
Font picker + font embedding in the Typography panel
nyblnete9e5ed7a101a
diff --git a/slides/src/editor/panels.ts b/slides/src/editor/panels.tsindex 5c0e2289..e0e5c5f6 100644--- a/slides/src/editor/panels.ts+++ b/slides/src/editor/panels.ts@@ -4,6 +4,7 @@import type { Store } from '../store'import { uid, type ShapeElement, type SlideElement, type TextElement, type TransitionKind } from '../model'+import { FONT_CHOICES, firstFamily, injectFonts } from '../fonts'import { ICONS } from '../icons'export class PropsPanel {@@ -369,6 +370,7 @@ export class PropsPanel {private buildTextProps(el: TextElement) {this.section('Typography')+ this.row('Font', this.fontSelect(el))this.row('Size', this.number(el.fontSize, 1, (v, fin) =>this.mutate(el.id, (e) => { (e as TextElement).fontSize = Math.max(v, 4) }, fin)))this.row('Weight', this.select(['300', '400', '600', '700', '800'], String(el.fontWeight), (v) =>@@ -381,6 +383,76 @@ export class PropsPanel {this.mutate(el.id, (e) => { (e as TextElement).valign = v as TextElement['valign'] }, true)))this.row('Line height', this.number(el.lineHeight, 0.05, (v, fin) =>this.mutate(el.id, (e) => { (e as TextElement).lineHeight = Math.max(v, 0.5) }, fin)))++ const embed = document.createElement('button')+ embed.className = 'ed-btn ed-btn-block'+ embed.textContent = '+ Embed font file…'+ embed.title = 'Bundle a .woff2/.woff/.ttf/.otf into this file and use it here'+ embed.addEventListener('click', () => this.embedFont(el))+ this.host.appendChild(embed)+ }++ /**+ * Font picker: theme default, curated system stacks, fonts embedded in the+ * document, and (if unmatched) the element's current custom stack. Options+ * render in their own face so the menu previews itself.+ */+ private fontSelect(el: TextElement): HTMLElement {+ const sel = document.createElement('select')+ const current = el.fontFamily ?? ''+ const add = (label: string, value: string, selected: boolean) => {+ const o = document.createElement('option')+ o.value = value+ o.textContent = label+ o.style.fontFamily = value || 'inherit'+ o.selected = selected+ sel.appendChild(o)+ return o+ }+ const curFirst = firstFamily(current)+ let matched = false+ add('theme default', '', current === '')+ for (const f of this.store.doc.fonts ?? []) {+ const hit = !matched && (current === f.family || curFirst === firstFamily(f.family))+ if (hit) matched = true+ add(`${f.family} (embedded)`, f.family, hit)+ }+ for (const c of FONT_CHOICES) {+ const hit = !matched && current !== '' && curFirst === firstFamily(c.stack)+ if (hit) matched = true+ add(c.label, c.stack, hit)+ }+ if (current && !matched) add(curFirst || 'custom', current, true)+ sel.addEventListener('change', () =>+ this.mutate(el.id, (e) => { (e as TextElement).fontFamily = sel.value }, true))+ return sel+ }++ /** Bundle a font file into the document and apply it to this element. */+ private embedFont(el: TextElement) {+ const input = document.createElement('input')+ input.type = 'file'+ input.accept = '.woff2,.woff,.ttf,.otf'+ input.addEventListener('change', () => {+ const file = input.files?.[0]+ if (!file) return+ const reader = new FileReader()+ reader.onload = () => {+ const family = file.name.replace(/\.[a-z0-9]+$/i, '').replace(/[-_]+/g, ' ').trim() || 'Embedded font'+ const asset = `font_${family.toLowerCase().replace(/[^a-z0-9]+/g, '-')}`+ this.store.commit(() => {+ const doc = this.store.doc+ doc.assets = { ...(doc.assets ?? {}), [asset]: String(reader.result) }+ doc.fonts = [...(doc.fonts ?? []).filter((f) => f.family !== family), { family, asset }]+ const live = this.store.element(el.id)+ if (live && live.type === 'text') live.fontFamily = family+ })+ injectFonts(this.store.doc)+ this.toast(`"${family}" embedded into this file`)+ }+ reader.readAsDataURL(file)+ })+ input.click()}private buildShapeProps(el: ShapeElement) {diff --git a/slides/src/fonts.ts b/slides/src/fonts.tsnew file mode 100644index 00000000..a279fd5f--- /dev/null+++ b/slides/src/fonts.ts@@ -0,0 +1,44 @@+// Font utilities: the curated system-stack choices offered in the editor,+// and @font-face injection for fonts embedded in the document's asset table.++import type { BentoDoc } from './model'++/** Safe cross-platform stacks offered in the font picker. */+export const FONT_CHOICES: Array<{ label: string; stack: string }> = [+ { label: 'System UI', stack: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif" },+ { label: 'Helvetica', stack: "'Helvetica Neue', Helvetica, Arial, sans-serif" },+ { label: 'Verdana', stack: "Verdana, 'DejaVu Sans', Geneva, Tahoma, sans-serif" },+ { label: 'Trebuchet', stack: "'Trebuchet MS', 'Segoe UI', Tahoma, sans-serif" },+ { label: 'Georgia', stack: "Georgia, 'Times New Roman', serif" },+ { label: 'Palatino', stack: "Palatino, 'Palatino Linotype', 'Book Antiqua', Georgia, serif" },+ { label: 'Times', stack: "'Times New Roman', Times, serif" },+ { label: 'Monospace', stack: "ui-monospace, 'SF Mono', Menlo, Consolas, 'Courier New', monospace" },+ { label: 'Impact', stack: "Impact, 'Arial Black', 'Franklin Gothic Bold', sans-serif" },+]++/** First family of a stack, normalised — used to match stacks loosely. */+export function firstFamily(stack: string): string {+ return (stack.split(',')[0] ?? '').trim().replace(/^['"]|['"]$/g, '').toLowerCase()+}++/**+ * (Re)register @font-face rules for every embedded font in the document.+ * Idempotent — call at boot and again whenever a font is added.+ */+export function injectFonts(doc: BentoDoc) {+ const css = (doc.fonts ?? [])+ .map((f) => {+ const src = doc.assets?.[f.asset]+ if (!src) return ''+ return `@font-face{font-family:${JSON.stringify(f.family)};src:url(${JSON.stringify(src)});` ++ `font-weight:${f.weight ?? 'normal'};font-style:${f.style ?? 'normal'};font-display:swap}`+ })+ .join('\n')+ let style = document.getElementById('bento-fonts') as HTMLStyleElement | null+ if (!style) {+ style = document.createElement('style')+ style.id = 'bento-fonts'+ document.head.appendChild(style)+ }+ style.textContent = css+}diff --git a/slides/src/main.ts b/slides/src/main.tsindex d5e62cd6..ad2ff3ab 100644--- a/slides/src/main.ts+++ b/slides/src/main.ts@@ -4,6 +4,7 @@import './styles.css'import { capturePristine, readEmbeddedDoc, serializeFile } from './save'import { parseDoc, starterDoc } from './model'+import { injectFonts } from './fonts'import { Store } from './store'import { Editor } from './editor/editor'@@ -16,20 +17,7 @@ document.title = `${doc.title} — Bento Slides`// Embedded fonts: register @font-face rules from the asset table so text// elements can use bundled families in the editor, presenter and thumbnails.-if (doc.fonts?.length) {- const css = doc.fonts- .map((f) => {- const src = doc.assets?.[f.asset]- if (!src) return ''- return `@font-face{font-family:${JSON.stringify(f.family)};src:url(${JSON.stringify(src)});` +- `font-weight:${f.weight ?? 'normal'};font-style:${f.style ?? 'normal'};font-display:swap}`- })- .join('\n')- const style = document.createElement('style')- style.id = 'bento-fonts'- style.textContent = css- document.head.appendChild(style)-}+if (doc.fonts?.length) injectFonts(doc)const store = new Store(doc)const editor = new Editor(document.getElementById('app')!, store)