squiffy-runtime 6.0.0-alpha.5 → 6.0.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,7 @@ interface SquiffyApi {
10
10
  get: (attribute: string) => any;
11
11
  set: (attribute: string, value: any) => void;
12
12
  clickLink: (link: HTMLElement) => void;
13
+ update: (story: Story) => void;
13
14
  }
14
15
  interface SquiffyJsFunctionApi {
15
16
  get: (attribute: string) => any;
@@ -2,6 +2,7 @@ export const init = (options) => {
2
2
  let story;
3
3
  let currentSection;
4
4
  let currentSectionElement;
5
+ let currentBlockOutputElement;
5
6
  let scrollPosition = 0;
6
7
  let outputElement;
7
8
  let settings;
@@ -42,7 +43,7 @@ export const init = (options) => {
42
43
  set('_turncount', get('_turncount') + 1);
43
44
  passage = processLink(passage);
44
45
  if (passage) {
45
- currentSectionElement?.appendChild(document.createElement('hr'));
46
+ newBlockOutputElement();
46
47
  showPassage(passage);
47
48
  }
48
49
  const turnPassage = '@' + get('_turncount');
@@ -56,7 +57,6 @@ export const init = (options) => {
56
57
  }
57
58
  }
58
59
  else if (section) {
59
- currentSectionElement?.appendChild(document.createElement('hr'));
60
60
  disableLink(link);
61
61
  section = processLink(section);
62
62
  if (section) {
@@ -201,24 +201,24 @@ export const init = (options) => {
201
201
  }, { once: true });
202
202
  labelElement.classList.add('fade-out');
203
203
  }
204
- function go(section) {
204
+ function go(sectionName) {
205
205
  set('_transition', null);
206
- newSection();
207
- currentSection = story.sections[section];
206
+ newSection(sectionName);
207
+ currentSection = story.sections[sectionName];
208
208
  if (!currentSection)
209
209
  return;
210
- set('_section', section);
211
- setSeen(section);
210
+ set('_section', sectionName);
211
+ setSeen(sectionName);
212
212
  const master = story.sections[''];
213
213
  if (master) {
214
214
  run(master);
215
- ui.write(master.text || '');
215
+ ui.write(master.text || '', "[[]]");
216
216
  }
217
217
  run(currentSection);
218
218
  // The JS might have changed which section we're in
219
- if (get('_section') == section) {
219
+ if (get('_section') == sectionName) {
220
220
  set('_turncount', 0);
221
- ui.write(currentSection.text || '');
221
+ ui.write(currentSection.text || '', `[[${sectionName}]]`);
222
222
  save();
223
223
  }
224
224
  }
@@ -255,16 +255,16 @@ export const init = (options) => {
255
255
  const masterPassage = masterSection.passages[''];
256
256
  if (masterPassage) {
257
257
  run(masterPassage);
258
- ui.write(masterPassage.text || '');
258
+ ui.write(masterPassage.text || '', `[[]][]`);
259
259
  }
260
260
  }
261
261
  const master = currentSection.passages && currentSection.passages[''];
262
262
  if (master) {
263
263
  run(master);
264
- ui.write(master.text || '');
264
+ ui.write(master.text || '', `[[${get("_section")}]][]`);
265
265
  }
266
266
  run(passage);
267
- ui.write(passage.text || '');
267
+ ui.write(passage.text || '', `[[${get("_section")}]][${passageName}]`);
268
268
  save();
269
269
  }
270
270
  function processAttributes(attributes) {
@@ -305,10 +305,8 @@ export const init = (options) => {
305
305
  if (!output)
306
306
  return false;
307
307
  outputElement.innerHTML = output;
308
- const element = document.getElementById(get('_output-section'));
309
- if (!element)
310
- return false;
311
- currentSectionElement = element;
308
+ currentSectionElement = outputElement.querySelector('.squiffy-output-section:last-child');
309
+ currentBlockOutputElement = outputElement.querySelector('.squiffy-output-block:last-child');
312
310
  currentSection = story.sections[get('_section')];
313
311
  const transition = get('_transition');
314
312
  if (transition) {
@@ -331,7 +329,12 @@ export const init = (options) => {
331
329
  return false;
332
330
  return (seenSections.indexOf(sectionName) > -1);
333
331
  }
334
- function newSection() {
332
+ function newBlockOutputElement() {
333
+ currentBlockOutputElement = document.createElement('div');
334
+ currentBlockOutputElement.classList.add('squiffy-output-block');
335
+ currentSectionElement?.appendChild(currentBlockOutputElement);
336
+ }
337
+ function newSection(sectionName) {
335
338
  if (currentSectionElement) {
336
339
  disableLinks(currentSectionElement.querySelectorAll('.squiffy-link'));
337
340
  currentSectionElement.querySelectorAll('input').forEach(el => {
@@ -357,23 +360,30 @@ export const init = (options) => {
357
360
  set('_section-count', sectionCount);
358
361
  const id = 'squiffy-section-' + sectionCount;
359
362
  currentSectionElement = document.createElement('div');
363
+ currentSectionElement.classList.add('squiffy-output-section');
360
364
  currentSectionElement.id = id;
365
+ if (sectionName) {
366
+ currentSectionElement.setAttribute('data-section', `${sectionName}`);
367
+ }
361
368
  outputElement.appendChild(currentSectionElement);
362
- set('_output-section', id);
369
+ newBlockOutputElement();
363
370
  }
364
371
  const ui = {
365
- write: (text) => {
366
- if (!currentSectionElement)
372
+ write: (text, source) => {
373
+ if (!currentBlockOutputElement)
367
374
  return;
368
375
  scrollPosition = outputElement.scrollHeight;
369
376
  const div = document.createElement('div');
370
- currentSectionElement.appendChild(div);
377
+ if (source) {
378
+ div.setAttribute('data-source', source);
379
+ }
380
+ currentBlockOutputElement.appendChild(div);
371
381
  div.innerHTML = ui.processText(text);
372
382
  ui.scrollToEnd();
373
383
  },
374
384
  clearScreen: () => {
375
385
  outputElement.innerHTML = '';
376
- newSection();
386
+ newSection(null);
377
387
  },
378
388
  scrollToEnd: () => {
379
389
  if (settings.scroll === 'element') {
@@ -573,6 +583,54 @@ export const init = (options) => {
573
583
  remaining += ':' + current;
574
584
  return [next, remaining];
575
585
  }
586
+ function getSectionContent(section) {
587
+ return outputElement.querySelectorAll(`[data-source='[[${section}]]']`);
588
+ }
589
+ function getPassageContent(section, passage) {
590
+ return outputElement.querySelectorAll(`[data-source='[[${section}]][${passage}]']`);
591
+ }
592
+ function update(newStory) {
593
+ // TODO: Re-disable clicked links after update
594
+ for (const existingSection of Object.keys(story.sections)) {
595
+ const elements = getSectionContent(existingSection);
596
+ if (elements.length) {
597
+ const newSection = newStory.sections[existingSection];
598
+ if (!newSection) {
599
+ // section has been deleted
600
+ for (const element of elements) {
601
+ element.remove();
602
+ }
603
+ }
604
+ else if (newSection.text && newSection.text != story.sections[existingSection].text) {
605
+ // section has been updated
606
+ for (const element of elements) {
607
+ element.innerHTML = ui.processText(newSection.text);
608
+ }
609
+ }
610
+ }
611
+ if (!story.sections[existingSection].passages)
612
+ continue;
613
+ for (const existingPassage of Object.keys(story.sections[existingSection].passages)) {
614
+ const elements = getPassageContent(existingSection, existingPassage);
615
+ if (!elements.length)
616
+ continue;
617
+ const newPassage = newStory.sections[existingSection]?.passages && newStory.sections[existingSection]?.passages[existingPassage];
618
+ if (!newPassage) {
619
+ // passage has been deleted
620
+ for (const element of elements) {
621
+ element.remove();
622
+ }
623
+ }
624
+ else if (newPassage.text && newPassage.text != story.sections[existingSection].passages[existingPassage].text) {
625
+ // passage has been updated
626
+ for (const element of elements) {
627
+ element.innerHTML = ui.processText(newPassage.text);
628
+ }
629
+ }
630
+ }
631
+ }
632
+ story = newStory;
633
+ }
576
634
  outputElement = options.element;
577
635
  story = options.story;
578
636
  settings = {
@@ -598,6 +656,7 @@ export const init = (options) => {
598
656
  restart: restart,
599
657
  get: get,
600
658
  set: set,
601
- clickLink: handleLink
659
+ clickLink: handleLink,
660
+ update: update,
602
661
  };
603
662
  };
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "squiffy-runtime",
3
- "version": "6.0.0-alpha.5",
3
+ "version": "6.0.0-alpha.6",
4
4
  "main": "dist/squiffy.runtime.js",
5
5
  "types": "dist/squiffy.runtime.d.ts",
6
6
  "scripts": {
7
7
  "test": "vitest",
8
- "build": "tsc"
8
+ "build": "tsc",
9
+ "prepublishOnly": "npm run build && npm run test"
9
10
  },
10
11
  "author": "Alex Warren",
11
12
  "contributors": [