@vonaffenfels/contentful-slate-editor 1.0.34 → 1.0.35

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vonaffenfels/contentful-slate-editor",
3
- "version": "1.0.34",
3
+ "version": "1.0.35",
4
4
  "scripts": {
5
5
  "prepublish": "yarn run build",
6
6
  "dev": "yarn run start",
@@ -89,10 +89,10 @@
89
89
  "webpack-dev-server": "^4.0.0-beta.2"
90
90
  },
91
91
  "dependencies": {
92
- "@vonaffenfels/slate-editor": "^1.0.34",
92
+ "@vonaffenfels/slate-editor": "^1.0.35",
93
93
  "webpack": "5.88.2"
94
94
  },
95
- "gitHead": "09afee2ada365a370687380dbcb426fdb6e4d51f",
95
+ "gitHead": "8531e3bdf7affdda47f286a5786cd7778044f1fc",
96
96
  "publishConfig": {
97
97
  "access": "public"
98
98
  }
@@ -13,6 +13,7 @@ import componentLoader from "@vonaffenfels/slate-editor/componentLoader";
13
13
 
14
14
  let isLoadingCountGlobal = 0;
15
15
  let loaderTimeout;
16
+ let isInitialLoad = true;
16
17
 
17
18
  const EditorField = ({
18
19
  fieldSdk,
@@ -65,6 +66,10 @@ const EditorField = ({
65
66
  if (windowSdk) {
66
67
  windowSdk.startAutoResizer();
67
68
  }
69
+
70
+ setTimeout(() => {
71
+ isInitialLoad = false;
72
+ }, 1000);
68
73
  }, []);
69
74
 
70
75
  useEffect(() => {
@@ -93,6 +98,55 @@ const EditorField = ({
93
98
  fakeActiveContent[field] = entrySdk.fields[field].getValue();
94
99
  }
95
100
 
101
+ const executeLoader = async (block, attributes) => {
102
+ let loaderResult = {};
103
+
104
+ setIsLoadingCount(++isLoadingCountGlobal);
105
+
106
+ try {
107
+ const fetchUrl = `/api/loader/?${new URLSearchParams({
108
+ portal: portal,
109
+ block: block,
110
+ variables: JSON.stringify({
111
+ ...attributes,
112
+ entries: null,
113
+ items: null,
114
+ }),
115
+ })}`;
116
+
117
+ loaderResult = await fetch(fetchUrl).then(res => res.json());
118
+
119
+ if (loaderResult) {
120
+ for (let key in loaderResult) {
121
+ attributes[key] = loaderResult[key];
122
+ }
123
+ }
124
+ } catch (e) {
125
+ console.error(`Error in loader for ${block}`);
126
+ console.error(e);
127
+ }
128
+
129
+ setIsLoadingCount(--isLoadingCountGlobal);
130
+
131
+ return loaderResult;
132
+ };
133
+
134
+ const getLoaderResult = async (block, attributes) => {
135
+ return new Promise((resolve, reject) => {
136
+ if (isInitialLoad) {
137
+ executeLoader(block, attributes).then(result => resolve(result));
138
+
139
+ return;
140
+ }
141
+
142
+ clearTimeout(loaderTimeout);
143
+
144
+ loaderTimeout = setTimeout(async () => {
145
+ resolve(await executeLoader(block, attributes));
146
+ }, 1000);
147
+ });
148
+ };
149
+
96
150
  return <div ref={wrapperRef} className="editor-field h-full">
97
151
  <BlockEditor
98
152
  onChange={onChange}
@@ -103,42 +157,7 @@ const EditorField = ({
103
157
  storybookComponentLoader={componentLoader}
104
158
  storybookStories={storybookStories}
105
159
  isLoading={isLoadingCount !== 0}
106
- storybookComponentDataLoader={async (block, attributes) => {
107
- let loaderResult = {};
108
-
109
- clearTimeout(loaderTimeout);
110
-
111
- loaderTimeout = await setTimeout(async () => {
112
- setIsLoadingCount(++isLoadingCountGlobal);
113
-
114
- try {
115
- const fetchUrl = `/api/loader/?${new URLSearchParams({
116
- portal: portal,
117
- block: block,
118
- variables: JSON.stringify({
119
- ...attributes,
120
- entries: null,
121
- items: null,
122
- }),
123
- })}`;
124
-
125
- loaderResult = await fetch(fetchUrl).then(res => res.json());
126
-
127
- if (loaderResult) {
128
- for (let key in loaderResult) {
129
- attributes[key] = loaderResult[key];
130
- }
131
- }
132
- } catch (e) {
133
- console.error(`Error in loader for ${block}`);
134
- console.error(e);
135
- }
136
-
137
- setIsLoadingCount(--isLoadingCountGlobal);
138
- }, 1000);
139
-
140
- return loaderResult;
141
- }}
160
+ storybookComponentDataLoader={async (block, attributes) => await getLoaderResult(block, attributes)}
142
161
  onStorybookElementClick={onStorybookElementClick}
143
162
  />
144
163
  </div>;