@atrament/web 2.1.2 → 2.1.4

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.
package/package.json CHANGED
@@ -1,15 +1,21 @@
1
1
  {
2
2
  "name": "@atrament/web",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "Atrament, a text game engine - build for web browsers",
5
- "main": "src/index.js",
5
+ "type": "module",
6
+ "main": "dist/index.mjs",
7
+ "exports": {
8
+ ".": {
9
+ "require": "./dist/index.cjs",
10
+ "import": "./dist/index.mjs"
11
+ }
12
+ },
6
13
  "files": [
7
- "src",
8
- "umd"
14
+ "dist"
9
15
  ],
10
16
  "scripts": {
17
+ "build": "vite build",
11
18
  "prepack": "npm run build",
12
- "build": "webpack --config webpack.config.babel.js --mode production",
13
19
  "lint": "eslint ./src/ --ext .js"
14
20
  },
15
21
  "author": "Serhii Mozhaiskyi (sergei.mozhaisky@gmail.com)",
@@ -19,28 +25,18 @@
19
25
  },
20
26
  "license": "MIT",
21
27
  "dependencies": {
22
- "@atrament/core": "2.0.0",
28
+ "@atrament/core": "2.1.4",
23
29
  "fflate": "0.8.2",
24
30
  "howler": "2.2.4",
25
- "jszip": "3.10.1",
26
31
  "localforage": "1.10.0",
27
32
  "murmurhash-js": "1.0.0",
28
- "nanostores": "1.0.1"
33
+ "nanostores": "1.1.0"
29
34
  },
30
35
  "devDependencies": {
31
- "@babel/core": "7.28.3",
32
- "@babel/eslint-parser": "7.28.0",
33
- "@babel/preset-env": "7.28.3",
34
- "@babel/register": "7.28.3",
35
- "babel-loader": "10.0.0",
36
36
  "eslint": "8.49.0",
37
37
  "eslint-config-airbnb-base": "15.0.0",
38
38
  "eslint-plugin-import": "2.32.0",
39
- "jest": "30.1.2",
40
- "jest-mock-console": "2.0.0",
41
39
  "path": "0.12.7",
42
- "terser-webpack-plugin": "5.3.14",
43
- "webpack": "5.101.3",
44
- "webpack-cli": "6.0.1"
40
+ "vite": "^7.3.1"
45
41
  }
46
42
  }
package/src/index.js DELETED
@@ -1,21 +0,0 @@
1
- /* global window */
2
-
3
- import atrament from '@atrament/core';
4
-
5
- import loader from './interfaces/loader-web-fflate';
6
- import persistentWeb from './interfaces/persistent-localforage';
7
- import persistentNeutralino from './interfaces/persistent-neutralino';
8
- import sound from './interfaces/sound-howler';
9
- import state from './interfaces/state-nanostores';
10
- import platformWeb from './interfaces/platform-web';
11
- import platformNeutralino from './interfaces/platform-neutralino';
12
-
13
- atrament.defineInterfaces({
14
- loader,
15
- persistent: window.Neutralino ? persistentNeutralino : persistentWeb,
16
- sound,
17
- state,
18
- platform: window.Neutralino ? platformNeutralino : platformWeb
19
- });
20
-
21
- export default atrament;
@@ -1,28 +0,0 @@
1
- /* global window, document */
2
-
3
- export async function loadFromJSON(filename) {
4
- const response = await fetch(filename);
5
- return response;
6
- }
7
-
8
- export async function appendScript(src) {
9
- return new Promise((resolve, reject) => {
10
- const el = document.createElement('script');
11
- el.src = src;
12
- el.addEventListener('load', resolve);
13
- el.addEventListener('error', reject);
14
- document.body.append(el);
15
- });
16
- }
17
-
18
- export async function loadFromJS(filename) {
19
- const response = {};
20
- try {
21
- await appendScript(filename);
22
- response.ok = true;
23
- response.text = () => JSON.stringify(window.storyContent);
24
- } catch (e) {
25
- console.error(e);
26
- }
27
- return response;
28
- }
@@ -1,26 +0,0 @@
1
- import { loadFromJS, loadFromJSON } from './loader-common';
2
-
3
- let $gamePath = '';
4
-
5
- async function init(path) {
6
- $gamePath = path;
7
- }
8
-
9
- function getAssetPath(filename) {
10
- return `${$gamePath}/${filename}`;
11
- }
12
-
13
- async function loadInk(filename) {
14
- const fullpath = getAssetPath(filename);
15
- const response = await (fullpath.endsWith('js') ? loadFromJS(fullpath) : loadFromJSON(fullpath));
16
- if (response.ok) {
17
- return response.text();
18
- }
19
- throw new Error(`Failed to load ink file: ${filename}`);
20
- }
21
-
22
- export default {
23
- init,
24
- getAssetPath,
25
- loadInk
26
- };
@@ -1,81 +0,0 @@
1
- /* eslint-disable no-await-in-loop */
2
-
3
- import { unzip } from 'fflate';
4
-
5
- import { loadFromJS, loadFromJSON } from './loader-common';
6
-
7
- let $gameFileType = 'plain';
8
- let $gamePath = '';
9
- let $zipContent = {};
10
-
11
- let $onProgress = () => {};
12
-
13
- async function fetchWithProgress(path) {
14
- const response = await fetch(path, { responseType: 'arraybuffer' });
15
- const reader = response.body.getReader();
16
- const contentLength = +response.headers.get('Content-Length');
17
- const chunks = [];
18
- let receivedLength = 0;
19
- for (;;) {
20
- const { done, value } = await reader.read();
21
- if (done) {
22
- break;
23
- }
24
- chunks.push(value);
25
- receivedLength += value.length;
26
- const percent = Math.round((receivedLength * 100) / contentLength);
27
- $onProgress({ percent, receivedLength, contentLength });
28
- }
29
- const chunksAll = new Uint8Array(receivedLength);
30
- let position = 0;
31
- chunks.forEach((chunk) => {
32
- chunksAll.set(chunk, position);
33
- position += chunk.length;
34
- });
35
- return chunksAll;
36
- }
37
-
38
- async function init(path) {
39
- if (path.endsWith('.zip')) {
40
- $gameFileType = 'zip';
41
- }
42
- if ($gameFileType === 'zip') {
43
- $zipContent = {}; // clear zip content
44
- const zipContent = await fetchWithProgress(path);
45
- await unzip(
46
- zipContent,
47
- (err, unzipped) => Object.entries(unzipped).forEach(([filepath, content]) => {
48
- $zipContent[filepath] = URL.createObjectURL(new Blob([content]));
49
- }));
50
- } else {
51
- $gamePath = path;
52
- }
53
- }
54
-
55
- function getAssetPath(filename) {
56
- if ($gameFileType === 'zip') {
57
- return $zipContent[filename];
58
- }
59
- return `${$gamePath}/${filename}`;
60
- }
61
-
62
-
63
- async function loadInk(filename) {
64
- const fullpath = getAssetPath(filename);
65
- const response = await (fullpath.endsWith('js') ? loadFromJS(fullpath) : loadFromJSON(fullpath));
66
- if (response.ok) {
67
- return response.text();
68
- }
69
- throw new Error(`Failed to load ink file: ${filename}`);
70
- }
71
-
72
- function onProgress(cb) {
73
- $onProgress = cb;
74
- }
75
-
76
- export default {
77
- init,
78
- getAssetPath,
79
- loadInk,
80
- onProgress
81
- };
@@ -1,56 +0,0 @@
1
- import JSZip from 'jszip';
2
-
3
- import { loadFromJS, loadFromJSON } from './loader-common';
4
-
5
- let $gameFileType = 'plain';
6
- let $gamePath = '';
7
- let $zipContent = {};
8
-
9
- async function $handleFile(path, entry) {
10
- if (entry.dir) {
11
- return;
12
- }
13
- const content = await entry.async('blob');
14
- const dataUrl = URL.createObjectURL(content);
15
- $zipContent[path] = dataUrl;
16
- }
17
-
18
- async function init(path) {
19
- if (path.endsWith('.zip')) {
20
- $gameFileType = 'zip';
21
- }
22
- if ($gameFileType === 'zip') {
23
- $zipContent = {}; // clear zip content
24
- const response = await fetch(path, { responseType: 'arraybuffer' });
25
- const zipFileContent = await JSZip.loadAsync(response.arrayBuffer());
26
- const fileHandlers = [];
27
- zipFileContent.forEach((relativePath, content) => {
28
- fileHandlers.push($handleFile(relativePath, content));
29
- });
30
- await Promise.all(fileHandlers);
31
- } else {
32
- $gamePath = path;
33
- }
34
- }
35
-
36
- function getAssetPath(filename) {
37
- if ($gameFileType === 'zip') {
38
- return $zipContent[filename];
39
- }
40
- return `${$gamePath}/${filename}`;
41
- }
42
-
43
- async function loadInk(filename) {
44
- const fullpath = getAssetPath(filename);
45
- const response = await (fullpath.endsWith('js') ? loadFromJS(fullpath) : loadFromJSON(fullpath));
46
- if (response.ok) {
47
- return response.text();
48
- }
49
- throw new Error(`Failed to load ink file: ${filename}`);
50
- }
51
-
52
- export default {
53
- init,
54
- getAssetPath,
55
- loadInk
56
- };
@@ -1,43 +0,0 @@
1
- import localforage from 'localforage';
2
-
3
- let storage;
4
-
5
- function init(prefix) {
6
- storage = localforage.createInstance(`${prefix}-atrament`);
7
- storage.config({
8
- name: prefix
9
- });
10
- storage.setDriver([localforage.INDEXEDDB, localforage.LOCALSTORAGE]);
11
- }
12
-
13
- async function exists(item) {
14
- const value = await storage.getItem(item);
15
- return value !== null;
16
- }
17
-
18
- async function get(item) {
19
- const value = await storage.getItem(item);
20
- return value;
21
- }
22
-
23
- async function set(item, value) {
24
- await storage.setItem(item, value);
25
- }
26
-
27
- async function remove(item) {
28
- await storage.removeItem(item);
29
- }
30
-
31
- async function keys() {
32
- const storageKeys = await storage.keys();
33
- return storageKeys;
34
- }
35
-
36
- export default {
37
- init,
38
- exists,
39
- get,
40
- set,
41
- remove,
42
- keys
43
- };
@@ -1,112 +0,0 @@
1
-
2
- /* global window */
3
- import murmur from 'murmurhash-js';
4
-
5
- const KEY_STORAGE = '__atrament_storage';
6
-
7
- // storage keys update queue
8
- let storageLock = Promise.resolve();
9
-
10
- // neutralino functions
11
-
12
- async function neuGetData(key) {
13
- try {
14
- const data = await window.Neutralino.storage.getData(key);
15
- return JSON.parse(data);
16
- } catch (e) {
17
- console.warn(e.message);
18
- return null;
19
- }
20
- }
21
-
22
- async function neuSetData(key, value) {
23
- const v = JSON.stringify(value);
24
- await window.Neutralino.storage.setData(key, v);
25
- return v;
26
- }
27
-
28
- async function neuDeleteData(key) {
29
- await window.Neutralino.storage.setData(key, null);
30
- return true;
31
- }
32
-
33
- // key handling
34
-
35
- function normalizeKey(key) {
36
- return `storage_${murmur(key)}`;
37
- }
38
-
39
- async function neuSetKey(key, item) {
40
- storageLock = storageLock.then(async () => {
41
- let storageKeys = await neuGetData(KEY_STORAGE);
42
- if (storageKeys === null) {
43
- storageKeys = {};
44
- }
45
- storageKeys[item] = key;
46
- await neuSetData(KEY_STORAGE, storageKeys);
47
- }).catch((e) => console.error(e.message));
48
- return storageLock;
49
- }
50
-
51
- async function neuDeleteKey(item) {
52
- storageLock = storageLock.then(async () => {
53
- let storageKeys = await neuGetData(KEY_STORAGE);
54
- if (storageKeys === null) {
55
- storageKeys = {};
56
- }
57
- delete storageKeys[item];
58
- await neuSetData(KEY_STORAGE, storageKeys);
59
- }).catch((e) => console.error(e.message));
60
- return storageLock;
61
- }
62
-
63
- // interface functions
64
-
65
- function init() {
66
- }
67
-
68
- async function exists(item) {
69
- const persistentKeys = await keys();
70
- return persistentKeys.includes(item);
71
- }
72
-
73
- async function get(item) {
74
- const storageKeys = await neuGetData(KEY_STORAGE);
75
- const key = storageKeys === null ? normalizeKey(item) : storageKeys[item];
76
- const value = await neuGetData(key);
77
- return value;
78
- }
79
-
80
- async function set(item, value) {
81
- const key = normalizeKey(item);
82
- try {
83
- await neuSetData(key, value);
84
- await neuSetKey(key, item);
85
- } catch (e) {
86
- console.error(e.message);
87
- }
88
- }
89
-
90
- async function remove(item) {
91
- const key = normalizeKey(item);
92
- try {
93
- await neuDeleteData(key);
94
- await neuDeleteKey(item);
95
- } catch (e) {
96
- console.error(e.message);
97
- }
98
- }
99
-
100
- async function keys() {
101
- const storageKeys = await neuGetData(KEY_STORAGE);
102
- return storageKeys === null ? [] : Object.keys(storageKeys);
103
- }
104
-
105
- export default {
106
- init,
107
- exists,
108
- get,
109
- set,
110
- remove,
111
- keys
112
- };
@@ -1,23 +0,0 @@
1
- /* global window */
2
-
3
- async function setFullscreen(enabled) {
4
- if (enabled) {
5
- await window.Neutralino.window.setFullScreen();
6
- } else {
7
- await window.Neutralino.window.exitFullScreen();
8
- }
9
- }
10
-
11
- async function setTitle(title) {
12
- await window.Neutralino.window.setTitle(title);
13
- }
14
-
15
- async function exitApp() {
16
- await window.Neutralino.app.exit();
17
- }
18
-
19
- export default {
20
- setFullscreen,
21
- setTitle,
22
- exitApp
23
- };
@@ -1,22 +0,0 @@
1
- /* global document */
2
-
3
- async function setFullscreen(enabled, setFullscreenState) {
4
- if (enabled && !document.fullscreenElement) {
5
- try {
6
- await document.documentElement.requestFullscreen();
7
- } catch {
8
- setFullscreenState(false);
9
- }
10
- } else if (document.fullscreenElement && document.exitFullscreen) {
11
- document.exitFullscreen();
12
- }
13
- }
14
-
15
- function setTitle(title) {
16
- document.title = title;
17
- }
18
-
19
- export default {
20
- setFullscreen,
21
- setTitle
22
- };
@@ -1,120 +0,0 @@
1
- import { Howl, Howler } from 'howler';
2
-
3
- const soundCache = {};
4
- const musicCache = {};
5
-
6
- // stop playback of given sound file or all cached sound files
7
- function $stopPlayback(file, cache) {
8
- if (file) {
9
- if (cache[file]) {
10
- cache[file].stop();
11
- }
12
- } else {
13
- Object.keys(cache).forEach((s) => {
14
- if (s && s.fade) {
15
- s.fade(undefined, 0, 1);
16
- }
17
- cache[s].stop();
18
- });
19
- }
20
- }
21
-
22
- /*
23
- init({
24
- mute: true/false,
25
- volume: 0-100
26
- }) - initialize sound with given configuration
27
- */
28
- function init(cfg) {
29
- mute(cfg.mute);
30
- setVolume(cfg.volume);
31
- }
32
-
33
- /*
34
- mute(true/false) - turn sound on/off
35
- */
36
- function mute(state = true) {
37
- Howler.mute(state);
38
- }
39
-
40
- /*
41
- isMuted(true/false) - returns true if sound is muted, false otherwise
42
- */
43
- function isMuted() {
44
- return Howler.muted;
45
- }
46
-
47
- /*
48
- setVolume(volume) - should set sound volume (0-100)
49
- */
50
- function setVolume(volume) {
51
- Howler.volume(volume / 100);
52
- }
53
-
54
- /*
55
- getVolume() - returns current volume
56
- */
57
- function getVolume() {
58
- return Howler.volume * 100;
59
- }
60
-
61
- /*
62
- playSound(snd)
63
- Plays sound once.
64
- - snd: path to a sound file.
65
- */
66
- function playSound(snd) {
67
- if (!soundCache[snd]) {
68
- soundCache[snd] = new Howl({
69
- src: [snd]
70
- });
71
- }
72
- soundCache[snd].play();
73
- }
74
-
75
- /*
76
- stopSound(snd)
77
- sound once.
78
- - snd: path to a sound file.
79
- If no parameter is supplied, all sounds should be stopped
80
- */
81
- function stopSound(snd) {
82
- $stopPlayback(snd, soundCache);
83
- }
84
-
85
- /*
86
- playMusic(mus, loop)
87
- - mus: path to a music file
88
- - loop: is track looped? true by default
89
- */
90
- function playMusic(mus, loop = true) {
91
- if (!musicCache[mus]) {
92
- musicCache[mus] = new Howl({
93
- src: [mus],
94
- html5: true, // streaming
95
- loop
96
- });
97
- }
98
- musicCache[mus].play();
99
- }
100
-
101
- /*
102
- stopMusic(mus)
103
- - mus: path to a music file
104
- If no parameter is supplied, all music should be stopped
105
- */
106
- function stopMusic(mus) {
107
- $stopPlayback(mus, musicCache);
108
- }
109
-
110
- export default {
111
- init,
112
- mute,
113
- isMuted,
114
- setVolume,
115
- getVolume,
116
- playSound,
117
- stopSound,
118
- playMusic,
119
- stopMusic
120
- };
@@ -1,58 +0,0 @@
1
- import { map } from 'nanostores';
2
-
3
- const $state = map({
4
- settings: {},
5
- game: {},
6
- metadata: {},
7
- scenes: [],
8
- vars: {}
9
- });
10
-
11
- function store() {
12
- return $state;
13
- }
14
-
15
- function get() {
16
- return $state.get();
17
- }
18
-
19
- const setKey = (key, value) => {
20
- $state.setKey(key, value);
21
- return $state.get();
22
- };
23
-
24
- const toggleKey = (key) => {
25
- $state.setKey(key, !$state.get()[key]);
26
- return $state.get();
27
- };
28
-
29
- const appendKey = (key, value) => {
30
- $state.setKey(key, [...$state.get()[key], value]);
31
- return $state.get();
32
- };
33
-
34
- const setSubkey = (key, subkey, value) => {
35
- $state.setKey(key, { ...$state.get()[key], [subkey]: value });
36
- return $state.get();
37
- };
38
-
39
- const toggleSubkey = (key, subkey) => {
40
- $state.setKey(key, { ...$state.get()[key], [subkey]: !$state.get()[key][subkey] });
41
- return $state.get();
42
- };
43
-
44
- const appendSubkey = (key, subkey, value) => {
45
- $state.setKey(key, { ...$state.get()[key], [subkey]: [...$state.get()[key][subkey], value] });
46
- return $state.get();
47
- };
48
-
49
- export default {
50
- store,
51
- get,
52
- setKey,
53
- toggleKey,
54
- appendKey,
55
- setSubkey,
56
- toggleSubkey,
57
- appendSubkey
58
- };