@mapwhit/tilerenderer 1.5.0 → 1.6.0

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.
@@ -1,4 +1,4 @@
1
1
  {
2
- "version": "1.5.0",
2
+ "version": "1.6.0",
3
3
  "type": "module"
4
4
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mapwhit/tilerenderer",
3
3
  "description": "A WebGL interactive maps library",
4
- "version": "1.5.0",
4
+ "version": "1.6.0",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": "./src/index.js",
@@ -25,7 +25,6 @@
25
25
  "@mapwhit/vector-tile": "4.0.0",
26
26
  "@pirxpilot/nanoassert": "~1",
27
27
  "csscolorparser": "^1.0.3",
28
- "dynload": "^1.0.2",
29
28
  "earcut": "^3.0.1",
30
29
  "geojson-vt": "^4.0.2",
31
30
  "grid-index": "^1.1.0",
package/src/index.js CHANGED
@@ -43,22 +43,27 @@ export default mapwhit;
43
43
  */
44
44
 
45
45
  /**
46
- * Sets the map's [RTL text plugin](https://www.mapbox.com/mapbox-gl-js/plugins/#mapbox-gl-rtl-text).
46
+ * Sets the map's [RTL text plugin](https://github.com/mapwhit/rtl-text).
47
47
  * Necessary for supporting languages like Arabic and Hebrew that are written right-to-left.
48
48
  *
49
49
  * @function setRTLTextPlugin
50
- * @param {string} pluginURL URL pointing to the Mapbox RTL text plugin source.
51
- * @param {boolean} lazy If set to `true`, mapboxgl will defer loading the plugin until rtl text is encountered,
50
+ * @param {function} loadPlugin a function that returns a Promise resolving to object
51
+ * with RTL text plugin methods `applyArabicShaping`, `processBidirectionalText`,
52
+ * and `processStyledBidirectionalText`.
53
+ * @param {boolean} lazy If set to `true`, loading the plugin will defer until rtl text is encountered,
52
54
  * rtl text will then be rendered only after the plugin finishes loading.
53
55
  * @example
54
- * setRTLTextPlugin('https://unpkg.com/@mapbox/mapbox-gl-rtl-text@0.3.0/dist/mapbox-gl-rtl-text.js', false);
56
+ * ```javascript
57
+ * import loadRTLTextPlugin from '@mapwhit/rtl-text';
58
+ * setRTLTextPlugin(loadRTLTextPlugin, true);
59
+ * ```
55
60
  * @see [Add support for right-to-left scripts](https://maplibre.org/maplibre-gl-js/docs/examples/mapbox-gl-rtl-text/)
56
61
  */
57
- function setRTLTextPlugin(pluginURL, lazy) {
58
- return rtlPluginLoader.setRTLTextPlugin(pluginURL, lazy);
62
+ function setRTLTextPlugin(loadPlugin, lazy) {
63
+ return rtlPluginLoader.setRTLTextPlugin(loadPlugin, lazy);
59
64
  }
60
65
  /**
61
- * Gets the map's [RTL text plugin](https://www.mapbox.com/mapbox-gl-js/plugins/#mapbox-gl-rtl-text) status.
66
+ * Gets the map's [RTL text plugin](https://github.com/mapwhit/rtl-text) status.
62
67
  * The status can be `unavailable` (i.e. not requested or removed), `loading`, `loaded` or `error`.
63
68
  * If the status is `loaded` and the plugin is requested again, an error will be thrown.
64
69
  *
@@ -1,6 +1,4 @@
1
1
  import { Event, Evented } from '@mapwhit/events';
2
- import dynload from 'dynload';
3
- import browser from '../util/browser.js';
4
2
 
5
3
  /**
6
4
  * The possible option of the plugin's status
@@ -49,14 +47,13 @@ export const rtlPlugin = RTLPlugin();
49
47
 
50
48
  function RTLPluginLoader() {
51
49
  let status = 'unavailable';
52
- let url;
50
+ let load;
53
51
 
54
52
  const self = {
55
53
  getRTLTextPluginStatus,
56
54
  setRTLTextPlugin,
57
55
  lazyLoad,
58
- _clearRTLTextPlugin,
59
- _registerRTLTextPlugin
56
+ _clearRTLTextPlugin
60
57
  };
61
58
 
62
59
  /** This one is exposed to outside */
@@ -66,20 +63,17 @@ function RTLPluginLoader() {
66
63
 
67
64
  // public for testing
68
65
  function _clearRTLTextPlugin() {
69
- url = undefined;
70
66
  status = 'unavailable';
67
+ load = undefined;
71
68
  _setMethods();
72
69
  }
73
70
 
74
- function setRTLTextPlugin(pluginURL, deferred = false) {
75
- if (url) {
71
+ function setRTLTextPlugin(pluginLoad, deferred = false) {
72
+ if (load) {
76
73
  // error
77
74
  return Promise.reject(new Error('setRTLTextPlugin cannot be called multiple times.'));
78
75
  }
79
- url = browser.resolveURL(pluginURL);
80
- if (!url) {
81
- return Promise.reject(new Error(`requested url ${pluginURL} is invalid`));
82
- }
76
+ load = pluginLoad;
83
77
  if (status === 'requested') {
84
78
  return _downloadRTLTextPlugin();
85
79
  }
@@ -93,9 +87,13 @@ function RTLPluginLoader() {
93
87
  }
94
88
 
95
89
  async function _downloadRTLTextPlugin() {
90
+ if (typeof load !== 'function') {
91
+ return Promise.reject(new Error('RTL text plugin load function is not set.'));
92
+ }
96
93
  status = 'loading';
97
94
  try {
98
- await rtlPluginLoader._loadScript({ url });
95
+ _setMethods(await load());
96
+ status = 'loaded';
99
97
  } catch {
100
98
  status = 'error';
101
99
  }
@@ -126,36 +124,7 @@ function RTLPluginLoader() {
126
124
  rtlPlugin.processStyledBidirectionalText = rtlTextPlugin.processStyledBidirectionalText;
127
125
  }
128
126
 
129
- // This is invoked by the RTL text plugin when the download has finished, and the code has been parsed.
130
- function _registerRTLTextPlugin(rtlTextPlugin) {
131
- if (rtlPlugin.isRTLSupported()) {
132
- throw new Error('RTL text plugin already registered.');
133
- }
134
- status = 'loaded';
135
- _setMethods(rtlTextPlugin);
136
- }
137
-
138
127
  return self;
139
128
  }
140
129
 
141
- // public for testing
142
- function _loadScript({ url }) {
143
- const { promise, resolve, reject } = Promise.withResolvers();
144
- const s = dynload(url);
145
- s.onload = () => resolve();
146
- s.onerror = () => reject(true);
147
- return promise;
148
- }
149
-
150
- const { getRTLTextPluginStatus, setRTLTextPlugin, lazyLoad, _clearRTLTextPlugin, _registerRTLTextPlugin } =
151
- RTLPluginLoader();
152
-
153
- globalThis.registerRTLTextPlugin ??= _registerRTLTextPlugin;
154
-
155
- export const rtlPluginLoader = Object.assign(new Evented(), {
156
- getRTLTextPluginStatus,
157
- setRTLTextPlugin,
158
- lazyLoad,
159
- _clearRTLTextPlugin,
160
- _loadScript
161
- });
130
+ export const rtlPluginLoader = Object.assign(new Evented(), RTLPluginLoader());