onchain-lexical-context 0.0.2 → 0.0.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.
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ */
8
+
9
+ 'use strict';
10
+
11
+ var yWebsocket = require('y-websocket');
12
+ var yjs = require('yjs');
13
+
14
+ /**
15
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
16
+ *
17
+ * This source code is licensed under the MIT license found in the
18
+ * LICENSE file in the root directory of this source tree.
19
+ *
20
+ */
21
+
22
+ const url = new URL(window.location.href);
23
+ const params = new URLSearchParams(url.search);
24
+ const WEBSOCKET_ENDPOINT = params.get('collabEndpoint') || 'ws://localhost:1234';
25
+ const WEBSOCKET_SLUG = 'playground';
26
+ const WEBSOCKET_ID = params.get('collabId') || '0';
27
+
28
+ // parent dom -> child doc
29
+ function createWebsocketProvider(id, yjsDocMap) {
30
+ let doc = yjsDocMap.get(id);
31
+ if (doc === undefined) {
32
+ doc = new yjs.Doc();
33
+ yjsDocMap.set(id, doc);
34
+ } else {
35
+ doc.load();
36
+ }
37
+
38
+ // @ts-expect-error
39
+ return new yWebsocket.WebsocketProvider(WEBSOCKET_ENDPOINT, WEBSOCKET_SLUG + '/' + WEBSOCKET_ID + '/' + id, doc, {
40
+ connect: false
41
+ });
42
+ }
43
+
44
+ exports.createWebsocketProvider = createWebsocketProvider;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ */
8
+
9
+ import { WebsocketProvider } from 'y-websocket';
10
+ import { Doc } from 'yjs';
11
+
12
+ /**
13
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
14
+ *
15
+ * This source code is licensed under the MIT license found in the
16
+ * LICENSE file in the root directory of this source tree.
17
+ *
18
+ */
19
+
20
+ const url = new URL(window.location.href);
21
+ const params = new URLSearchParams(url.search);
22
+ const WEBSOCKET_ENDPOINT = params.get('collabEndpoint') || 'ws://localhost:1234';
23
+ const WEBSOCKET_SLUG = 'playground';
24
+ const WEBSOCKET_ID = params.get('collabId') || '0';
25
+
26
+ // parent dom -> child doc
27
+ function createWebsocketProvider(id, yjsDocMap) {
28
+ let doc = yjsDocMap.get(id);
29
+ if (doc === undefined) {
30
+ doc = new Doc();
31
+ yjsDocMap.set(id, doc);
32
+ } else {
33
+ doc.load();
34
+ }
35
+
36
+ // @ts-expect-error
37
+ return new WebsocketProvider(WEBSOCKET_ENDPOINT, WEBSOCKET_SLUG + '/' + WEBSOCKET_ID + '/' + id, doc, {
38
+ connect: false
39
+ });
40
+ }
41
+
42
+ export { createWebsocketProvider };
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ */
8
+
9
+ 'use strict';
10
+
11
+ var FlashMessage = require('onchain-lexical-ui/FlashMessage');
12
+ var react = require('react');
13
+ var jsxRuntime = require('react/jsx-runtime');
14
+
15
+ /**
16
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
17
+ *
18
+ * This source code is licensed under the MIT license found in the
19
+ * LICENSE file in the root directory of this source tree.
20
+ *
21
+ */
22
+
23
+ const Context = /*#__PURE__*/react.createContext(undefined);
24
+ const INITIAL_STATE = {};
25
+ const DEFAULT_DURATION = 1000;
26
+ const FlashMessageContext = ({
27
+ children
28
+ }) => {
29
+ const [props, setProps] = react.useState(INITIAL_STATE);
30
+ const showFlashMessage = react.useCallback((message, duration) => setProps(message ? {
31
+ duration,
32
+ message
33
+ } : INITIAL_STATE), []);
34
+ react.useEffect(() => {
35
+ if (props.message) {
36
+ const timeoutId = setTimeout(() => setProps(INITIAL_STATE), props.duration ?? DEFAULT_DURATION);
37
+ return () => clearTimeout(timeoutId);
38
+ }
39
+ }, [props]);
40
+ return /*#__PURE__*/jsxRuntime.jsxs(Context.Provider, {
41
+ value: showFlashMessage,
42
+ children: [children, props.message && /*#__PURE__*/jsxRuntime.jsx(FlashMessage, {
43
+ children: props.message
44
+ })]
45
+ });
46
+ };
47
+ const useFlashMessageContext = () => {
48
+ const ctx = react.useContext(Context);
49
+ if (!ctx) {
50
+ throw new Error('Missing FlashMessageContext');
51
+ }
52
+ return ctx;
53
+ };
54
+
55
+ exports.FlashMessageContext = FlashMessageContext;
56
+ exports.useFlashMessageContext = useFlashMessageContext;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ */
8
+
9
+ import FlashMessage from 'onchain-lexical-ui/FlashMessage';
10
+ import { useState, useCallback, useEffect, createContext, useContext } from 'react';
11
+ import { jsxs, jsx } from 'react/jsx-runtime';
12
+
13
+ /**
14
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
15
+ *
16
+ * This source code is licensed under the MIT license found in the
17
+ * LICENSE file in the root directory of this source tree.
18
+ *
19
+ */
20
+
21
+ const Context = /*#__PURE__*/createContext(undefined);
22
+ const INITIAL_STATE = {};
23
+ const DEFAULT_DURATION = 1000;
24
+ const FlashMessageContext = ({
25
+ children
26
+ }) => {
27
+ const [props, setProps] = useState(INITIAL_STATE);
28
+ const showFlashMessage = useCallback((message, duration) => setProps(message ? {
29
+ duration,
30
+ message
31
+ } : INITIAL_STATE), []);
32
+ useEffect(() => {
33
+ if (props.message) {
34
+ const timeoutId = setTimeout(() => setProps(INITIAL_STATE), props.duration ?? DEFAULT_DURATION);
35
+ return () => clearTimeout(timeoutId);
36
+ }
37
+ }, [props]);
38
+ return /*#__PURE__*/jsxs(Context.Provider, {
39
+ value: showFlashMessage,
40
+ children: [children, props.message && /*#__PURE__*/jsx(FlashMessage, {
41
+ children: props.message
42
+ })]
43
+ });
44
+ };
45
+ const useFlashMessageContext = () => {
46
+ const ctx = useContext(Context);
47
+ if (!ctx) {
48
+ throw new Error('Missing FlashMessageContext');
49
+ }
50
+ return ctx;
51
+ };
52
+
53
+ export { FlashMessageContext, useFlashMessageContext };
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ */
8
+
9
+ 'use strict';
10
+
11
+ var React = require('react');
12
+ var jsxRuntime = require('react/jsx-runtime');
13
+
14
+ function _interopNamespaceDefault(e) {
15
+ var n = Object.create(null);
16
+ if (e) {
17
+ for (var k in e) {
18
+ n[k] = e[k];
19
+ }
20
+ }
21
+ n.default = e;
22
+ return n;
23
+ }
24
+
25
+ var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
26
+
27
+ /**
28
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
29
+ *
30
+ * This source code is licensed under the MIT license found in the
31
+ * LICENSE file in the root directory of this source tree.
32
+ *
33
+ */
34
+ /* eslint-disable @typescript-eslint/no-explicit-any */
35
+
36
+ const Context = /*#__PURE__*/React.createContext({});
37
+ const InstanceConfigContext = ({
38
+ children,
39
+ value
40
+ }) => {
41
+ const [selectedInstance, setSelectedInstance] = React__namespace.useState([]);
42
+ const [instanceMap, setInstanceMap] = React__namespace.useState(new Map());
43
+ return /*#__PURE__*/jsxRuntime.jsx(Context.Provider, {
44
+ value: {
45
+ ...value,
46
+ instanceMap,
47
+ selectedInstance,
48
+ setInstanceMap,
49
+ setSelectedInstance
50
+ },
51
+ children: children
52
+ });
53
+ };
54
+ const useInstanceConfig = () => {
55
+ return React.useContext(Context);
56
+ };
57
+
58
+ exports.InstanceConfigContext = InstanceConfigContext;
59
+ exports.useInstanceConfig = useInstanceConfig;
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ */
8
+
9
+ import * as React from 'react';
10
+ import { createContext, useContext } from 'react';
11
+ import { jsx } from 'react/jsx-runtime';
12
+
13
+ /**
14
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
15
+ *
16
+ * This source code is licensed under the MIT license found in the
17
+ * LICENSE file in the root directory of this source tree.
18
+ *
19
+ */
20
+ /* eslint-disable @typescript-eslint/no-explicit-any */
21
+
22
+ const Context = /*#__PURE__*/createContext({});
23
+ const InstanceConfigContext = ({
24
+ children,
25
+ value
26
+ }) => {
27
+ const [selectedInstance, setSelectedInstance] = React.useState([]);
28
+ const [instanceMap, setInstanceMap] = React.useState(new Map());
29
+ return /*#__PURE__*/jsx(Context.Provider, {
30
+ value: {
31
+ ...value,
32
+ instanceMap,
33
+ selectedInstance,
34
+ setInstanceMap,
35
+ setSelectedInstance
36
+ },
37
+ children: children
38
+ });
39
+ };
40
+ const useInstanceConfig = () => {
41
+ return useContext(Context);
42
+ };
43
+
44
+ export { InstanceConfigContext, useInstanceConfig };
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ */
8
+
9
+ 'use strict';
10
+
11
+ var base64 = require('onchain-utility/base64');
12
+ var traversal = require('onchain-utility/traversal');
13
+
14
+ /**
15
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
16
+ *
17
+ * This source code is licensed under the MIT license found in the
18
+ * LICENSE file in the root directory of this source tree.
19
+ *
20
+ */
21
+
22
+ const DEFAULT_SETTINGS = {
23
+ disableBeforeInput: false,
24
+ emptyEditor: VITE_IS_DEVELOPMENT,
25
+ hasLinkAttributes: false,
26
+ isAutocomplete: false,
27
+ isCharLimit: false,
28
+ isCharLimitUtf8: false,
29
+ isCollab: false,
30
+ isMaxLength: false,
31
+ isRichText: true,
32
+ listStrictIndent: false,
33
+ measureTypingPerf: false,
34
+ selectionAlwaysOnDisplay: false,
35
+ shouldAllowHighlightingWithBrackets: false,
36
+ shouldPreserveNewLinesInMarkdown: false,
37
+ shouldUseLexicalContextMenu: false,
38
+ showNestedEditorTreeView: false,
39
+ showTableOfContents: false,
40
+ tableCellBackgroundColor: true,
41
+ tableCellMerge: true,
42
+ tableHorizontalScroll: true
43
+ };
44
+
45
+ // These are mutated in setupEnv
46
+ const INITIAL_SETTINGS = {
47
+ ...DEFAULT_SETTINGS
48
+ };
49
+
50
+ /**
51
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
52
+ *
53
+ * This source code is licensed under the MIT license found in the
54
+ * LICENSE file in the root directory of this source tree.
55
+ *
56
+ */
57
+
58
+ const uploadMap = new Map();
59
+ const base64SrcMap = new Map();
60
+ function deleteUpload(nodeKey) {
61
+ return base64SrcMap.delete(nodeKey);
62
+ }
63
+ function getUpload(nodeKey) {
64
+ const base64Src = base64SrcMap.get(nodeKey);
65
+ if (base64Src) {
66
+ return uploadMap.get(base64Src);
67
+ } else {
68
+ return undefined;
69
+ }
70
+ }
71
+ function hasUpload(nodeKey) {
72
+ const base64Src = base64SrcMap.get(nodeKey);
73
+ if (base64Src) {
74
+ return uploadMap.has(base64Src);
75
+ } else {
76
+ return false;
77
+ }
78
+ }
79
+ function upload({
80
+ nodeKey,
81
+ src,
82
+ hasError,
83
+ uploadFiles
84
+ }) {
85
+ const {
86
+ type,
87
+ suffix
88
+ } = base64.base64ToMimeType(src);
89
+ if (!hasError && type && suffix) {
90
+ const _upload = () => {
91
+ const result = uploadFiles([{
92
+ suffix,
93
+ text: src,
94
+ type
95
+ }]);
96
+ base64SrcMap.set(nodeKey, src);
97
+ uploadMap.set(src, {
98
+ result,
99
+ upload: _upload
100
+ });
101
+ return result;
102
+ };
103
+ if (!uploadMap.has(src)) {
104
+ _upload();
105
+ } else {
106
+ base64SrcMap.set(nodeKey, src);
107
+ }
108
+ }
109
+ }
110
+ async function markdownBase64StringToUpLoadUrl(markdown) {
111
+ const base64Regular = base64.getBase64Regular();
112
+ const base64List = Array.from(markdown.match(base64Regular) || []);
113
+ for (const base64 of base64List) {
114
+ if (uploadMap.has(base64)) {
115
+ markdown = markdown.replace(base64, (await uploadMap.get(base64).result)[0]);
116
+ }
117
+ }
118
+ return markdown;
119
+ }
120
+ async function jsonBase64StringToUpLoadUrl(node) {
121
+ await traversal.asyncDfs([node], async node => {
122
+ const src = node.src;
123
+ if (src && uploadMap.has(src)) {
124
+ node.src = (await uploadMap.get(src).result)[0];
125
+ }
126
+ return node.children || [];
127
+ });
128
+ return node;
129
+ }
130
+
131
+ exports.DEFAULT_SETTINGS = DEFAULT_SETTINGS;
132
+ exports.INITIAL_SETTINGS = INITIAL_SETTINGS;
133
+ exports.deleteUpload = deleteUpload;
134
+ exports.getUpload = getUpload;
135
+ exports.hasUpload = hasUpload;
136
+ exports.jsonBase64StringToUpLoadUrl = jsonBase64StringToUpLoadUrl;
137
+ exports.markdownBase64StringToUpLoadUrl = markdownBase64StringToUpLoadUrl;
138
+ exports.upload = upload;
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ */
8
+
9
+ import { base64ToMimeType, getBase64Regular } from 'onchain-utility/base64';
10
+ import { asyncDfs } from 'onchain-utility/traversal';
11
+
12
+ /**
13
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
14
+ *
15
+ * This source code is licensed under the MIT license found in the
16
+ * LICENSE file in the root directory of this source tree.
17
+ *
18
+ */
19
+
20
+ const DEFAULT_SETTINGS = {
21
+ disableBeforeInput: false,
22
+ emptyEditor: VITE_IS_DEVELOPMENT,
23
+ hasLinkAttributes: false,
24
+ isAutocomplete: false,
25
+ isCharLimit: false,
26
+ isCharLimitUtf8: false,
27
+ isCollab: false,
28
+ isMaxLength: false,
29
+ isRichText: true,
30
+ listStrictIndent: false,
31
+ measureTypingPerf: false,
32
+ selectionAlwaysOnDisplay: false,
33
+ shouldAllowHighlightingWithBrackets: false,
34
+ shouldPreserveNewLinesInMarkdown: false,
35
+ shouldUseLexicalContextMenu: false,
36
+ showNestedEditorTreeView: false,
37
+ showTableOfContents: false,
38
+ tableCellBackgroundColor: true,
39
+ tableCellMerge: true,
40
+ tableHorizontalScroll: true
41
+ };
42
+
43
+ // These are mutated in setupEnv
44
+ const INITIAL_SETTINGS = {
45
+ ...DEFAULT_SETTINGS
46
+ };
47
+
48
+ /**
49
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
50
+ *
51
+ * This source code is licensed under the MIT license found in the
52
+ * LICENSE file in the root directory of this source tree.
53
+ *
54
+ */
55
+
56
+ const uploadMap = new Map();
57
+ const base64SrcMap = new Map();
58
+ function deleteUpload(nodeKey) {
59
+ return base64SrcMap.delete(nodeKey);
60
+ }
61
+ function getUpload(nodeKey) {
62
+ const base64Src = base64SrcMap.get(nodeKey);
63
+ if (base64Src) {
64
+ return uploadMap.get(base64Src);
65
+ } else {
66
+ return undefined;
67
+ }
68
+ }
69
+ function hasUpload(nodeKey) {
70
+ const base64Src = base64SrcMap.get(nodeKey);
71
+ if (base64Src) {
72
+ return uploadMap.has(base64Src);
73
+ } else {
74
+ return false;
75
+ }
76
+ }
77
+ function upload({
78
+ nodeKey,
79
+ src,
80
+ hasError,
81
+ uploadFiles
82
+ }) {
83
+ const {
84
+ type,
85
+ suffix
86
+ } = base64ToMimeType(src);
87
+ if (!hasError && type && suffix) {
88
+ const _upload = () => {
89
+ const result = uploadFiles([{
90
+ suffix,
91
+ text: src,
92
+ type
93
+ }]);
94
+ base64SrcMap.set(nodeKey, src);
95
+ uploadMap.set(src, {
96
+ result,
97
+ upload: _upload
98
+ });
99
+ return result;
100
+ };
101
+ if (!uploadMap.has(src)) {
102
+ _upload();
103
+ } else {
104
+ base64SrcMap.set(nodeKey, src);
105
+ }
106
+ }
107
+ }
108
+ async function markdownBase64StringToUpLoadUrl(markdown) {
109
+ const base64Regular = getBase64Regular();
110
+ const base64List = Array.from(markdown.match(base64Regular) || []);
111
+ for (const base64 of base64List) {
112
+ if (uploadMap.has(base64)) {
113
+ markdown = markdown.replace(base64, (await uploadMap.get(base64).result)[0]);
114
+ }
115
+ }
116
+ return markdown;
117
+ }
118
+ async function jsonBase64StringToUpLoadUrl(node) {
119
+ await asyncDfs([node], async node => {
120
+ const src = node.src;
121
+ if (src && uploadMap.has(src)) {
122
+ node.src = (await uploadMap.get(src).result)[0];
123
+ }
124
+ return node.children || [];
125
+ });
126
+ return node;
127
+ }
128
+
129
+ export { DEFAULT_SETTINGS, INITIAL_SETTINGS, deleteUpload, getUpload, hasUpload, jsonBase64StringToUpLoadUrl, markdownBase64StringToUpLoadUrl, upload };
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ */
8
+
9
+ 'use strict';
10
+
11
+ var react = require('react');
12
+ var jsxRuntime = require('react/jsx-runtime');
13
+
14
+ /**
15
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
16
+ *
17
+ * This source code is licensed under the MIT license found in the
18
+ * LICENSE file in the root directory of this source tree.
19
+ *
20
+ */
21
+
22
+ const DEFAULT_SETTINGS = {
23
+ disableBeforeInput: false,
24
+ emptyEditor: VITE_IS_DEVELOPMENT,
25
+ hasLinkAttributes: false,
26
+ isAutocomplete: false,
27
+ isCharLimit: false,
28
+ isCharLimitUtf8: false,
29
+ isCollab: false,
30
+ isMaxLength: false,
31
+ isRichText: true,
32
+ listStrictIndent: false,
33
+ measureTypingPerf: false,
34
+ selectionAlwaysOnDisplay: false,
35
+ shouldAllowHighlightingWithBrackets: false,
36
+ shouldPreserveNewLinesInMarkdown: false,
37
+ shouldUseLexicalContextMenu: false,
38
+ showNestedEditorTreeView: false,
39
+ showTableOfContents: false,
40
+ tableCellBackgroundColor: true,
41
+ tableCellMerge: true,
42
+ tableHorizontalScroll: true
43
+ };
44
+
45
+ // These are mutated in setupEnv
46
+ const INITIAL_SETTINGS = {
47
+ ...DEFAULT_SETTINGS
48
+ };
49
+
50
+ /**
51
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
52
+ *
53
+ * This source code is licensed under the MIT license found in the
54
+ * LICENSE file in the root directory of this source tree.
55
+ *
56
+ */
57
+
58
+ const Context = /*#__PURE__*/react.createContext({
59
+ extra: {},
60
+ setOption: (name, value) => {
61
+ return;
62
+ },
63
+ settings: INITIAL_SETTINGS
64
+ });
65
+ const SettingsContext = ({
66
+ children,
67
+ extra
68
+ }) => {
69
+ const [settings, setSettings] = react.useState(INITIAL_SETTINGS);
70
+ const setOption = react.useCallback((setting, value) => {
71
+ setSettings(options => ({
72
+ ...options,
73
+ [setting]: value
74
+ }));
75
+ setURLParam(setting, value);
76
+ }, []);
77
+ const contextValue = react.useMemo(() => {
78
+ return {
79
+ extra,
80
+ setOption,
81
+ settings
82
+ };
83
+ }, [setOption, settings, extra]);
84
+ return /*#__PURE__*/jsxRuntime.jsx(Context.Provider, {
85
+ value: contextValue,
86
+ children: children
87
+ });
88
+ };
89
+ const useSettings = () => {
90
+ return react.useContext(Context);
91
+ };
92
+ function setURLParam(param, value) {
93
+ const url = new URL(window.location.href);
94
+ const params = new URLSearchParams(url.search);
95
+ if (value !== DEFAULT_SETTINGS[param]) {
96
+ params.set(param, String(value));
97
+ } else {
98
+ params.delete(param);
99
+ }
100
+ url.search = params.toString();
101
+ window.history.pushState(null, '', url.toString());
102
+ }
103
+
104
+ exports.SettingsContext = SettingsContext;
105
+ exports.useSettings = useSettings;
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ */
8
+
9
+ import { useState, useCallback, useMemo, createContext, useContext } from 'react';
10
+ import { jsx } from 'react/jsx-runtime';
11
+
12
+ /**
13
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
14
+ *
15
+ * This source code is licensed under the MIT license found in the
16
+ * LICENSE file in the root directory of this source tree.
17
+ *
18
+ */
19
+
20
+ const DEFAULT_SETTINGS = {
21
+ disableBeforeInput: false,
22
+ emptyEditor: VITE_IS_DEVELOPMENT,
23
+ hasLinkAttributes: false,
24
+ isAutocomplete: false,
25
+ isCharLimit: false,
26
+ isCharLimitUtf8: false,
27
+ isCollab: false,
28
+ isMaxLength: false,
29
+ isRichText: true,
30
+ listStrictIndent: false,
31
+ measureTypingPerf: false,
32
+ selectionAlwaysOnDisplay: false,
33
+ shouldAllowHighlightingWithBrackets: false,
34
+ shouldPreserveNewLinesInMarkdown: false,
35
+ shouldUseLexicalContextMenu: false,
36
+ showNestedEditorTreeView: false,
37
+ showTableOfContents: false,
38
+ tableCellBackgroundColor: true,
39
+ tableCellMerge: true,
40
+ tableHorizontalScroll: true
41
+ };
42
+
43
+ // These are mutated in setupEnv
44
+ const INITIAL_SETTINGS = {
45
+ ...DEFAULT_SETTINGS
46
+ };
47
+
48
+ /**
49
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
50
+ *
51
+ * This source code is licensed under the MIT license found in the
52
+ * LICENSE file in the root directory of this source tree.
53
+ *
54
+ */
55
+
56
+ const Context = /*#__PURE__*/createContext({
57
+ extra: {},
58
+ setOption: (name, value) => {
59
+ return;
60
+ },
61
+ settings: INITIAL_SETTINGS
62
+ });
63
+ const SettingsContext = ({
64
+ children,
65
+ extra
66
+ }) => {
67
+ const [settings, setSettings] = useState(INITIAL_SETTINGS);
68
+ const setOption = useCallback((setting, value) => {
69
+ setSettings(options => ({
70
+ ...options,
71
+ [setting]: value
72
+ }));
73
+ setURLParam(setting, value);
74
+ }, []);
75
+ const contextValue = useMemo(() => {
76
+ return {
77
+ extra,
78
+ setOption,
79
+ settings
80
+ };
81
+ }, [setOption, settings, extra]);
82
+ return /*#__PURE__*/jsx(Context.Provider, {
83
+ value: contextValue,
84
+ children: children
85
+ });
86
+ };
87
+ const useSettings = () => {
88
+ return useContext(Context);
89
+ };
90
+ function setURLParam(param, value) {
91
+ const url = new URL(window.location.href);
92
+ const params = new URLSearchParams(url.search);
93
+ if (value !== DEFAULT_SETTINGS[param]) {
94
+ params.set(param, String(value));
95
+ } else {
96
+ params.delete(param);
97
+ }
98
+ url.search = params.toString();
99
+ window.history.pushState(null, '', url.toString());
100
+ }
101
+
102
+ export { SettingsContext, useSettings };
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ */
8
+
9
+ 'use strict';
10
+
11
+ var LexicalHistoryPlugin = require('@lexical/react/LexicalHistoryPlugin');
12
+ var react = require('react');
13
+ var jsxRuntime = require('react/jsx-runtime');
14
+
15
+ /**
16
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
17
+ *
18
+ * This source code is licensed under the MIT license found in the
19
+ * LICENSE file in the root directory of this source tree.
20
+ *
21
+ */
22
+
23
+ const Context = /*#__PURE__*/react.createContext({});
24
+ const SharedHistoryContext = ({
25
+ children
26
+ }) => {
27
+ const historyContext = react.useMemo(() => ({
28
+ historyState: LexicalHistoryPlugin.createEmptyHistoryState()
29
+ }), []);
30
+ return /*#__PURE__*/jsxRuntime.jsx(Context.Provider, {
31
+ value: historyContext,
32
+ children: children
33
+ });
34
+ };
35
+ const useSharedHistoryContext = () => {
36
+ return react.useContext(Context);
37
+ };
38
+
39
+ exports.SharedHistoryContext = SharedHistoryContext;
40
+ exports.useSharedHistoryContext = useSharedHistoryContext;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ */
8
+
9
+ import { createEmptyHistoryState } from '@lexical/react/LexicalHistoryPlugin';
10
+ import { useMemo, createContext, useContext } from 'react';
11
+ import { jsx } from 'react/jsx-runtime';
12
+
13
+ /**
14
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
15
+ *
16
+ * This source code is licensed under the MIT license found in the
17
+ * LICENSE file in the root directory of this source tree.
18
+ *
19
+ */
20
+
21
+ const Context = /*#__PURE__*/createContext({});
22
+ const SharedHistoryContext = ({
23
+ children
24
+ }) => {
25
+ const historyContext = useMemo(() => ({
26
+ historyState: createEmptyHistoryState()
27
+ }), []);
28
+ return /*#__PURE__*/jsx(Context.Provider, {
29
+ value: historyContext,
30
+ children: children
31
+ });
32
+ };
33
+ const useSharedHistoryContext = () => {
34
+ return useContext(Context);
35
+ };
36
+
37
+ export { SharedHistoryContext, useSharedHistoryContext };
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ */
8
+
9
+ 'use strict';
10
+
11
+ var react = require('react');
12
+ var jsxRuntime = require('react/jsx-runtime');
13
+
14
+ /**
15
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
16
+ *
17
+ * This source code is licensed under the MIT license found in the
18
+ * LICENSE file in the root directory of this source tree.
19
+ *
20
+ */
21
+
22
+ const MIN_ALLOWED_FONT_SIZE = 8;
23
+ const MAX_ALLOWED_FONT_SIZE = 72;
24
+ const DEFAULT_FONT_SIZE = 15;
25
+ const blockTypeToBlockName = {
26
+ Paragraph: '正文',
27
+ Quote: '引用',
28
+ bullet: '列表',
29
+ check: '待办列表',
30
+ code: '代码块',
31
+ h1: '标题 1',
32
+ h2: '标题 2',
33
+ h3: '标题 3',
34
+ h4: '标题 4',
35
+ h5: '标题 5',
36
+ h6: '标题 6',
37
+ number: '列表',
38
+ paragraph: '正文',
39
+ quote: '引用'
40
+ };
41
+
42
+ //disable eslint sorting rule for quick reference to toolbar state
43
+ /* eslint-disable sort-keys-fix/sort-keys-fix */
44
+ const INITIAL_TOOLBAR_STATE = {
45
+ bgColor: '#fff',
46
+ blockType: 'Paragraph',
47
+ canRedo: false,
48
+ canUndo: false,
49
+ codeLanguage: '',
50
+ elementFormat: 'left',
51
+ fontColor: '#000',
52
+ fontFamily: 'Arial',
53
+ // Current font size in px
54
+ fontSize: `${DEFAULT_FONT_SIZE}px`,
55
+ // Font size input value - for controlled input
56
+ fontSizeInputValue: `${DEFAULT_FONT_SIZE}`,
57
+ isBold: false,
58
+ isCode: false,
59
+ isHighlight: false,
60
+ isImageCaption: false,
61
+ isItalic: false,
62
+ isLink: false,
63
+ isRTL: false,
64
+ isStrikethrough: false,
65
+ isSubscript: false,
66
+ isSuperscript: false,
67
+ isUnderline: false,
68
+ isLowercase: false,
69
+ isUppercase: false,
70
+ isCapitalize: false,
71
+ rootType: 'root'
72
+ };
73
+
74
+ // Utility type to get keys and infer value types
75
+
76
+ const Context = /*#__PURE__*/react.createContext(undefined);
77
+ const ToolbarContext = ({
78
+ children
79
+ }) => {
80
+ const [toolbarState, setToolbarState] = react.useState(INITIAL_TOOLBAR_STATE);
81
+ const selectionFontSize = toolbarState.fontSize;
82
+ const updateToolbarState = react.useCallback((key, value) => {
83
+ setToolbarState(prev => ({
84
+ ...prev,
85
+ [key]: value
86
+ }));
87
+ }, []);
88
+ react.useEffect(() => {
89
+ updateToolbarState('fontSizeInputValue', selectionFontSize.slice(0, -2));
90
+ }, [selectionFontSize, updateToolbarState]);
91
+ const contextValue = react.useMemo(() => {
92
+ return {
93
+ toolbarState,
94
+ updateToolbarState
95
+ };
96
+ }, [toolbarState, updateToolbarState]);
97
+ return /*#__PURE__*/jsxRuntime.jsx(Context.Provider, {
98
+ value: contextValue,
99
+ children: children
100
+ });
101
+ };
102
+ const useToolbarState = () => {
103
+ const context = react.useContext(Context);
104
+ if (context === undefined) {
105
+ throw new Error('useToolbarState must be used within a ToolbarProvider');
106
+ }
107
+ return context;
108
+ };
109
+
110
+ exports.DEFAULT_FONT_SIZE = DEFAULT_FONT_SIZE;
111
+ exports.MAX_ALLOWED_FONT_SIZE = MAX_ALLOWED_FONT_SIZE;
112
+ exports.MIN_ALLOWED_FONT_SIZE = MIN_ALLOWED_FONT_SIZE;
113
+ exports.ToolbarContext = ToolbarContext;
114
+ exports.blockTypeToBlockName = blockTypeToBlockName;
115
+ exports.useToolbarState = useToolbarState;
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ */
8
+
9
+ import { useState, useCallback, useEffect, useMemo, createContext, useContext } from 'react';
10
+ import { jsx } from 'react/jsx-runtime';
11
+
12
+ /**
13
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
14
+ *
15
+ * This source code is licensed under the MIT license found in the
16
+ * LICENSE file in the root directory of this source tree.
17
+ *
18
+ */
19
+
20
+ const MIN_ALLOWED_FONT_SIZE = 8;
21
+ const MAX_ALLOWED_FONT_SIZE = 72;
22
+ const DEFAULT_FONT_SIZE = 15;
23
+ const blockTypeToBlockName = {
24
+ Paragraph: '正文',
25
+ Quote: '引用',
26
+ bullet: '列表',
27
+ check: '待办列表',
28
+ code: '代码块',
29
+ h1: '标题 1',
30
+ h2: '标题 2',
31
+ h3: '标题 3',
32
+ h4: '标题 4',
33
+ h5: '标题 5',
34
+ h6: '标题 6',
35
+ number: '列表',
36
+ paragraph: '正文',
37
+ quote: '引用'
38
+ };
39
+
40
+ //disable eslint sorting rule for quick reference to toolbar state
41
+ /* eslint-disable sort-keys-fix/sort-keys-fix */
42
+ const INITIAL_TOOLBAR_STATE = {
43
+ bgColor: '#fff',
44
+ blockType: 'Paragraph',
45
+ canRedo: false,
46
+ canUndo: false,
47
+ codeLanguage: '',
48
+ elementFormat: 'left',
49
+ fontColor: '#000',
50
+ fontFamily: 'Arial',
51
+ // Current font size in px
52
+ fontSize: `${DEFAULT_FONT_SIZE}px`,
53
+ // Font size input value - for controlled input
54
+ fontSizeInputValue: `${DEFAULT_FONT_SIZE}`,
55
+ isBold: false,
56
+ isCode: false,
57
+ isHighlight: false,
58
+ isImageCaption: false,
59
+ isItalic: false,
60
+ isLink: false,
61
+ isRTL: false,
62
+ isStrikethrough: false,
63
+ isSubscript: false,
64
+ isSuperscript: false,
65
+ isUnderline: false,
66
+ isLowercase: false,
67
+ isUppercase: false,
68
+ isCapitalize: false,
69
+ rootType: 'root'
70
+ };
71
+
72
+ // Utility type to get keys and infer value types
73
+
74
+ const Context = /*#__PURE__*/createContext(undefined);
75
+ const ToolbarContext = ({
76
+ children
77
+ }) => {
78
+ const [toolbarState, setToolbarState] = useState(INITIAL_TOOLBAR_STATE);
79
+ const selectionFontSize = toolbarState.fontSize;
80
+ const updateToolbarState = useCallback((key, value) => {
81
+ setToolbarState(prev => ({
82
+ ...prev,
83
+ [key]: value
84
+ }));
85
+ }, []);
86
+ useEffect(() => {
87
+ updateToolbarState('fontSizeInputValue', selectionFontSize.slice(0, -2));
88
+ }, [selectionFontSize, updateToolbarState]);
89
+ const contextValue = useMemo(() => {
90
+ return {
91
+ toolbarState,
92
+ updateToolbarState
93
+ };
94
+ }, [toolbarState, updateToolbarState]);
95
+ return /*#__PURE__*/jsx(Context.Provider, {
96
+ value: contextValue,
97
+ children: children
98
+ });
99
+ };
100
+ const useToolbarState = () => {
101
+ const context = useContext(Context);
102
+ if (context === undefined) {
103
+ throw new Error('useToolbarState must be used within a ToolbarProvider');
104
+ }
105
+ return context;
106
+ };
107
+
108
+ export { DEFAULT_FONT_SIZE, MAX_ALLOWED_FONT_SIZE, MIN_ALLOWED_FONT_SIZE, ToolbarContext, blockTypeToBlockName, useToolbarState };
package/package.json CHANGED
@@ -1,15 +1,19 @@
1
1
  {
2
2
  "name": "onchain-lexical-context",
3
3
  "license": "MIT",
4
- "version": "0.0.2",
4
+ "version": "0.0.4",
5
5
  "dependencies": {
6
6
  "@lexical/react": "^0.30.0",
7
7
  "@lexical/yjs": "^0.30.0",
8
8
  "lexical": "0.30.0",
9
- "onchain-lexical-ui": "^0.0.2",
10
- "onchain-utility": "^0.0.3",
9
+ "onchain-lexical-ui": "^0.0.4",
10
+ "onchain-utility": "^0.0.5",
11
11
  "y-websocket": "^1.5.4"
12
12
  },
13
+ "files": [
14
+ "dist",
15
+ "src"
16
+ ],
13
17
  "exports": {
14
18
  ".": {
15
19
  "import": {
package/src/types.d.ts CHANGED
@@ -30,6 +30,7 @@ export interface BuiltInInstanceConfig {
30
30
 
31
31
  export interface InstanceConfigLet {
32
32
  loading: boolean;
33
+ verifyPermissions: boolean;
33
34
  table: React.MutableRefObject<{
34
35
  initSerializedData: string;
35
36
  cancelModification?: () => void;
@@ -39,6 +40,9 @@ export interface InstanceConfig {
39
40
  namespace: string;
40
41
  icl: InstanceConfigLet;
41
42
  setIcl(params: Partial<InstanceConfigLet>): void;
43
+ components?: {
44
+ TrackLinkList?: (props: {number: string}) => JSX.Element;
45
+ };
42
46
  preview?: boolean;
43
47
  getInstanceIcon: (objectApicode: string) => string;
44
48
  uploadFiles(