@atrament/web 2.0.0 → 2.1.1

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/README.md CHANGED
@@ -12,7 +12,7 @@ If you are looking for example of a web application based on Atrament, check out
12
12
 
13
13
  or
14
14
 
15
- ```<script src="https://unpkg.com/@atrament/web@2.0.0/umd/atrament.js"></script>```
15
+ ```<script src="https://unpkg.com/@atrament/web@2.1.1/umd/atrament.js"></script>```
16
16
 
17
17
  ## Usage
18
18
 
@@ -44,14 +44,18 @@ See [Atrament core documentation](https://github.com/technix/atrament-core/blob/
44
44
 
45
45
  ## Components
46
46
 
47
- - loader: fetch
48
- - persistent: [localForage](https://github.com/localForage/localForage)
47
+ - loader: fetch + fflate/jszip
48
+ - persistent: [localForage](https://github.com/localForage/localForage) / Neutralinojs storage
49
49
  - state: [nanostores](https://github.com/nanostores/nanostores)
50
50
  - sound: [howler.js](https://github.com/goldfire/howler.js)
51
-
51
+ - platform: web / Neutralinojs
52
52
 
53
53
  ## LICENSE
54
54
 
55
55
  Atrament is distributed under MIT license.
56
56
 
57
57
  Copyright (c) 2023 Serhii "techniX" Mozhaiskyi
58
+
59
+ Made with the support of the [Interactive Fiction Technology Foundation](https://iftechfoundation.org/)
60
+
61
+ <img src="https://iftechfoundation.org/logo.svg" width="200px">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atrament/web",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "description": "Atrament, a text game engine - build for web browsers",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -24,6 +24,7 @@
24
24
  "howler": "2.2.4",
25
25
  "jszip": "3.10.1",
26
26
  "localforage": "1.10.0",
27
+ "murmurhash-js": "1.0.0",
27
28
  "nanostores": "0.11.2"
28
29
  },
29
30
  "devDependencies": {
package/src/index.js CHANGED
@@ -1,15 +1,21 @@
1
+ /* global window */
2
+
1
3
  import atrament from '@atrament/core';
2
4
 
3
5
  import loader from './interfaces/loader-web-fflate';
4
- import persistent from './interfaces/persistent-localforage';
6
+ import persistentWeb from './interfaces/persistent-localforage';
7
+ import persistentNeutralino from './interfaces/persistent-neutralino';
5
8
  import sound from './interfaces/sound-howler';
6
9
  import state from './interfaces/state-nanostores';
10
+ import platformWeb from './interfaces/platform-web';
11
+ import platformNeutralino from './interfaces/platform-neutralino';
7
12
 
8
13
  atrament.defineInterfaces({
9
14
  loader,
10
- persistent,
15
+ persistent: window.Neutralino ? persistentNeutralino : persistentWeb,
11
16
  sound,
12
- state
17
+ state,
18
+ platform: window.Neutralino ? platformNeutralino : platformWeb
13
19
  });
14
20
 
15
21
  export default atrament;
@@ -0,0 +1,112 @@
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
+ };
@@ -0,0 +1,23 @@
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
+ };
@@ -0,0 +1,22 @@
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
+ };