Skip to content

Commit 2794f26

Browse files
author
Evgeny Kuzyakov
authored
Merge pull request #142 from NearSocial/release-2.5.0
## 2.5.0 - Fix `default` case for the switch statement in `VM`. - Add a VM feature, `enableComponentSrcDataKey`, which adds the `data-component` attribute specifying the path of the comonent responsible for rendering the DOM element. - Add support for VM.require when using redirectMap. - Fixes an issue with VM.require not retaining context in migration to initGlobalFunctions. - Add `onLink` and `onImage` to Markdown component. It allows to display links and images differently. - Expose all VM functions into the state directly, it simplifies VM readability and implementation. - Expose certain native objects directly into the state. It should improve access to the functions. - Update the way events and errors are passed to the functions. - For events, expose `preventDefault()` and `stopPropagation()` functions. NOTE: Previously, all React's `SyntheticEvent`s were getting `preventDefault()` called by default. - For errors, expose `message`, `name` and `type`. - Fix `vm.depth` not being initialized. - Introduce `useMemo` hook. Similar to the React hook, it calculates a value and memoizes it, only recalculating when one of its dependencies changes. ```jsx const data = [ //...some large array ]; const filteredData = useMemo(() => { console.log("Filtering data"); return data.filter(/* some filtering criteria */); }, [data]); return ( <div> {filteredData.map(item => ( <div key={item.id}>{item.name}</div> ))} </div> ); ``` - Introduce `useCallback` hook. Similarly to the React hook, it memoizes a callback function and returns that memoized version unless one of its dependencies changes. ```jsx const incrementCounter = useCallback(() => { setCounter(counter + 1); }, [counter]); return ( <div> Counter = {counter} <div> <button onClick={incrementCounter}>Increment</button> </div> </div> ); ```
2 parents c8d62ac + e3178b0 commit 2794f26

File tree

8 files changed

+775
-604
lines changed

8 files changed

+775
-604
lines changed

CHANGELOG.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,56 @@
11
# Changelog
22

3+
## 2.5.0
4+
5+
- Fix `default` case for the switch statement in `VM`.
6+
- Add a VM feature, `enableComponentSrcDataKey`, which adds the `data-component` attribute specifying the path of the comonent responsible for rendering the DOM element.
7+
- Add support for VM.require when using redirectMap.
8+
- Fixes an issue with VM.require not retaining context in migration to initGlobalFunctions.
9+
- Add `onLink` and `onImage` to Markdown component. It allows to display links and images differently.
10+
- Expose all VM functions into the state directly, it simplifies VM readability and implementation.
11+
- Expose certain native objects directly into the state. It should improve access to the functions.
12+
- Update the way events and errors are passed to the functions.
13+
- For events, expose `preventDefault()` and `stopPropagation()` functions.
14+
NOTE: Previously, all React's `SyntheticEvent`s were getting `preventDefault()` called by default.
15+
- For errors, expose `message`, `name` and `type`.
16+
- Fix `vm.depth` not being initialized.
17+
- Introduce `useMemo` hook. Similar to the React hook, it calculates a value and memoizes it, only recalculating when one of its dependencies changes.
18+
19+
```jsx
20+
const data = [
21+
//...some large array
22+
];
23+
24+
const filteredData = useMemo(() => {
25+
console.log("Filtering data");
26+
return data.filter(/* some filtering criteria */);
27+
}, [data]);
28+
29+
return (
30+
<div>
31+
{filteredData.map(item => (
32+
<div key={item.id}>{item.name}</div>
33+
))}
34+
</div>
35+
);
36+
```
37+
38+
- Introduce `useCallback` hook. Similarly to the React hook, it memoizes a callback function and returns that memoized version unless one of its dependencies changes.
39+
```jsx
40+
const incrementCounter = useCallback(() => {
41+
setCounter(counter + 1);
42+
}, [counter]);
43+
44+
return (
45+
<div>
46+
Counter = {counter}
47+
<div>
48+
<button onClick={incrementCounter}>Increment</button>
49+
</div>
50+
</div>
51+
);
52+
```
53+
354
## 2.4.2
455

556
- Add missing code changes (`cacheOptions` and `lodash`) from 2.4.0.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "near-social-vm",
3-
"version": "2.4.2",
3+
"version": "2.5.0",
44
"description": "Near Social VM",
55
"main": "dist/index.js",
66
"files": [

src/lib/components/Markdown.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@ import mentions from "./remark/mentions";
77
import hashtags from "./remark/hashtags";
88

99
export const Markdown = (props) => {
10-
const { onLinkClick, text, onMention, onHashtag, syntaxHighlighterProps } =
11-
props;
10+
const {
11+
onLink,
12+
onLinkClick,
13+
text,
14+
onMention,
15+
onHashtag,
16+
onImage,
17+
syntaxHighlighterProps,
18+
} = props;
1219
return (
1320
<ReactMarkdown
1421
plugins={[]}
@@ -25,12 +32,19 @@ export const Markdown = (props) => {
2532
return <strong {...props}>{children}</strong>;
2633
},
2734
a: ({ node, ...props }) =>
28-
onLinkClick ? (
35+
onLink ? (
36+
onLink({ ...props, href: node.properties?.href })
37+
) : onLinkClick ? (
2938
<a onClick={onLinkClick} {...props} />
3039
) : (
3140
<a target="_blank" {...props} />
3241
),
33-
img: ({ node, ...props }) => <img className="img-fluid" {...props} />,
42+
img: ({ node, ...props }) =>
43+
onImage ? (
44+
onImage({ ...props })
45+
) : (
46+
<img className="img-fluid" {...props} />
47+
),
3448
blockquote: ({ node, ...props }) => (
3549
<blockquote className="blockquote" {...props} />
3650
),

src/lib/components/Widget.js

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
isFunction,
1818
Loading,
1919
TGas,
20+
computeSrcOrCode,
2021
} from "../data/utils";
2122
import { ErrorBoundary } from "react-error-boundary";
2223
import { useCache } from "../data/cache";
@@ -26,27 +27,6 @@ import Big from "big.js";
2627
import uuid from "react-uuid";
2728
import { EthersProviderContext } from "./ethers";
2829

29-
const computeSrcOrCode = (src, code, configs) => {
30-
let srcOrCode = src ? { src } : code ? { code } : null;
31-
for (const config of configs || []) {
32-
if (srcOrCode?.src) {
33-
const src = srcOrCode.src;
34-
let value = isObject(config?.redirectMap) && config.redirectMap[src];
35-
if (!value) {
36-
try {
37-
value = isFunction(config?.redirect) && config.redirect(src);
38-
} catch {}
39-
}
40-
if (isString(value)) {
41-
srcOrCode = { src: value };
42-
} else if (isString(value?.code)) {
43-
return { code: value.code };
44-
}
45-
}
46-
}
47-
return srcOrCode;
48-
};
49-
5030
export const Widget = React.forwardRef((props, forwardedRef) => {
5131
const {
5232
loading,

src/lib/data/near.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,18 @@ async function web4ViewCall(contractId, methodName, args, fallback) {
211211
}
212212
}
213213

214+
/**
215+
* Current VM Features:
216+
* - enableComponentSrcDataKey: Allows enabling the component source `data-component` attribute for rendered DOM elements. Disabled by default.
217+
**/
214218
async function _initNear({
215219
networkId,
216220
config,
217221
keyStore,
218222
selector,
219223
walletConnectCallback = () => {},
220224
customElements = {},
225+
features = {},
221226
}) {
222227
if (!config) {
223228
config = {};
@@ -250,6 +255,7 @@ async function _initNear({
250255
selector,
251256
keyStore,
252257
nearConnection,
258+
features
253259
};
254260

255261
_near.nearArchivalConnection = nearAPI.Connection.fromConfig({

src/lib/data/utils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,4 +395,33 @@ export const deepCopy = (o) => {
395395
}
396396
};
397397

398+
export const filterValues = (o) => {
399+
return isObject(o)
400+
? Object.fromEntries(
401+
Object.entries(o).filter(([key, value]) => value !== undefined)
402+
)
403+
: o;
404+
};
405+
398406
export const deepEqual = equal;
407+
408+
export const computeSrcOrCode = (src, code, configs) => {
409+
let srcOrCode = src ? { src } : code ? { code } : null;
410+
for (const config of configs || []) {
411+
if (srcOrCode?.src) {
412+
const src = srcOrCode.src;
413+
let value = isObject(config?.redirectMap) && config.redirectMap[src];
414+
if (!value) {
415+
try {
416+
value = isFunction(config?.redirect) && config.redirect(src);
417+
} catch {}
418+
}
419+
if (isString(value)) {
420+
srcOrCode = { src: value };
421+
} else if (isString(value?.code)) {
422+
return { code: value.code };
423+
}
424+
}
425+
}
426+
return srcOrCode;
427+
};

0 commit comments

Comments
 (0)