| 1 | // fixtures/slide-layouts.mjs — what "New slide" puts on a slide. |
| 2 | // |
| 3 | // A layout here is a starting point, not a kind of slide: one of the |
| 4 | // seven flow templates plus the empty blocks a person fills in, all |
| 5 | // written through the ordinary ops (add_slide, then add_block per |
| 6 | // block). Nothing about a layout survives on the slide except the |
| 7 | // template and the blocks it made, so a slide made from "Title and |
| 8 | // list" is a title-body slide with a heading and a list on it, and the |
| 9 | // inspector changes it like any other. |
| 10 | // |
| 11 | // The blocks start empty; the stage shows each one's placeholder |
| 12 | // (render.Options.Placeholders, editor.css) so there is somewhere to |
| 13 | // click. A layout that gave a slide no block to type into would open |
| 14 | // on a blank rectangle — which is what "New slide" did before this. |
| 15 | import { newID } from './ids.mjs' |
| 16 | |
| 17 | const heading = (region, level = 1) => ({ type: 'heading', region, props: { level } }) |
| 18 | const text = (region, props) => ({ type: 'text', region, props }) |
| 19 | |
| 20 | /** Every layout the picker offers, in its order: name → {template, blocks}. */ |
| 21 | export const LAYOUTS = { |
| 22 | title: { template: 'title', blocks: [heading('heading'), text('body', { size: 'l' })] }, |
| 23 | 'title-text': { template: 'title-body', blocks: [heading('heading'), text('body')] }, |
| 24 | 'title-list': { template: 'title-body', blocks: [heading('heading'), { type: 'list', region: 'body', props: { style: 'bullet' } }] }, |
| 25 | 'two-column': { template: 'two-column', blocks: [heading('heading'), text('body'), text('aside')] }, |
| 26 | section: { template: 'section', blocks: [heading('heading')] }, |
| 27 | picture: { template: 'image-full', blocks: [{ type: 'image', region: 'media', props: { fit: 'cover' } }, heading('heading', 2)] }, |
| 28 | quote: { template: 'quote', blocks: [{ type: 'quote', region: 'body', props: {} }] }, |
| 29 | blank: { template: 'blank', blocks: [] } |
| 30 | } |
| 31 | |
| 32 | export const LAYOUT_NAMES = Object.keys(LAYOUTS) |
| 33 | |
| 34 | /** |
| 35 | * The layout a plain "New slide" uses after a slide with this template: |
| 36 | * the same again, except after an opening title (a deck's second slide |
| 37 | * is almost never another title) or after a blank one. |
| 38 | */ |
| 39 | export function layoutAfter (template) { |
| 40 | switch (template) { |
| 41 | case 'two-column': return 'two-column' |
| 42 | case 'section': return 'section' |
| 43 | case 'image-full': return 'picture' |
| 44 | case 'quote': return 'quote' |
| 45 | default: return 'title-text' |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * The op batch that makes slide `id` from `layout`, after `after` when |
| 51 | * it is given (null puts it first; undefined, last). Unknown layouts |
| 52 | * are 'title-text', so a stale menu item still makes a slide. |
| 53 | */ |
| 54 | export function layoutOps (id, layout, after, mint = newID) { |
| 55 | const l = LAYOUTS[layout] ?? LAYOUTS['title-text'] |
| 56 | const add = { op: 'add_slide', id, layout: 'flow', template: l.template } |
| 57 | if (after !== undefined) add.after = after |
| 58 | const ops = [add] |
| 59 | for (const b of l.blocks) { |
| 60 | ops.push({ op: 'add_block', slide: id, id: mint(), type: b.type, region: b.region, props: { ...b.props }, markdown: '' }) |
| 61 | } |
| 62 | return ops |
| 63 | } |
| 64 | |