@ohos-ports/react-native-assets 1.0.0-beta.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.
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@ohos-ports/react-native-assets",
3
+ "version": "1.0.0-beta.0",
4
+ "description": "Asset support code for React Native (OpenHarmony port).",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/ohos-ports/ohos-ports.git",
8
+ "directory": "ports/react-native-assets/1.0.0"
9
+ },
10
+ "license": "MIT",
11
+ "main": "registry.js",
12
+ "bugs": {
13
+ "url": "https://github.com/ohos-ports/ohos-ports/issues"
14
+ },
15
+ "files": [
16
+ "registry.js",
17
+ "path-support.js",
18
+ "README.md",
19
+ "LICENSE"
20
+ ]
21
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @format
8
+ */
9
+
10
+ 'use strict';
11
+
12
+ const androidScaleSuffix = {
13
+ '0.75': 'ldpi',
14
+ '1': 'mdpi',
15
+ '1.5': 'hdpi',
16
+ '2': 'xhdpi',
17
+ '3': 'xxhdpi',
18
+ '4': 'xxxhdpi',
19
+ };
20
+
21
+ /**
22
+ * FIXME: using number to represent discrete scale numbers is fragile in essence because of
23
+ * floating point numbers imprecision.
24
+ */
25
+ function getAndroidAssetSuffix(scale) {
26
+ if (scale.toString() in androidScaleSuffix) {
27
+ return androidScaleSuffix[scale.toString()];
28
+ }
29
+
30
+ throw new Error('no such scale ' + scale.toString());
31
+ }
32
+
33
+ // See https://developer.android.com/guide/topics/resources/drawable-resource.html
34
+ const drawableFileTypes = new Set([
35
+ 'gif',
36
+ 'jpeg',
37
+ 'jpg',
38
+ 'png',
39
+ 'svg',
40
+ 'webp',
41
+ 'xml',
42
+ ]);
43
+
44
+ function getAndroidResourceFolderName(asset, scale) {
45
+ if (!drawableFileTypes.has(asset.type)) {
46
+ return 'raw';
47
+ }
48
+ const suffix = getAndroidAssetSuffix(scale);
49
+ if (!suffix) {
50
+ throw new Error(
51
+ "Don't know which android drawable suffix to use for scale: " +
52
+ scale +
53
+ '\nAsset: ' +
54
+ JSON.stringify(asset, null, '\t') +
55
+ '\nPossible scales are:' +
56
+ JSON.stringify(androidScaleSuffix, null, '\t'),
57
+ );
58
+ }
59
+ return 'drawable-' + suffix;
60
+ }
61
+
62
+ function getAndroidResourceIdentifier(asset) {
63
+ return (getBasePath(asset) + '/' + asset.name)
64
+ .toLowerCase()
65
+ .replace(/\//g, '_') // Encode folder structure in file name
66
+ .replace(/([^a-z0-9_])/g, '') // Remove illegal chars
67
+ .replace(/^assets_/, ''); // Remove "assets_" prefix
68
+ }
69
+
70
+ function getBasePath(asset) {
71
+ const basePath = asset.httpServerLocation;
72
+ return basePath.startsWith('/') ? basePath.substr(1) : basePath;
73
+ }
74
+
75
+ module.exports = {
76
+ getAndroidResourceFolderName,
77
+ getAndroidResourceIdentifier,
78
+ getBasePath,
79
+ };
package/registry.js ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @format
8
+ */
9
+
10
+ 'use strict';
11
+
12
+ /**
13
+ * Type: PackagerAsset (Flow type stripped for plain JS compatibility).
14
+ * Shape: { __packager_asset: boolean, fileSystemLocation: string,
15
+ * httpServerLocation: string, width: number|null, height: number|null,
16
+ * scales: Array<number>, hash: string, name: string, type: string }
17
+ */
18
+
19
+ const assets = [];
20
+
21
+ function registerAsset(asset) {
22
+ // `push` returns new array length, so the first asset will
23
+ // get id 1 (not 0) to make the value truthy
24
+ return assets.push(asset);
25
+ }
26
+
27
+ function getAssetByID(assetId) {
28
+ return assets[assetId - 1];
29
+ }
30
+
31
+ module.exports = {registerAsset, getAssetByID};