Skip to content

Commit 69dd6e8

Browse files
committed
Move Fragment to SDK
Signed-off-by: Zvonimir Fras <zvonimir@zvonimirfras.com>
1 parent 5ea0377 commit 69dd6e8

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

app/src/components/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export * from './header';
22
export * from './main';
3-
export * from './fragment';
43
export * from './grid';
54
export * from './notification';

app/src/routes/edit/index.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import React, {
44
useState
55
} from 'react';
66
import { css, cx } from 'emotion';
7-
import { Fragment } from '../../components';
87
import { EditHeader } from './edit-header';
98
import { GlobalStateContext } from '../../context/global-state-context';
109
import {
@@ -37,12 +36,14 @@ import { CodeContextPane } from './code-context-pane';
3736
import { useParams } from 'react-router-dom';
3837
import { useHotkeys } from 'react-hotkeys-hook';
3938
import {
39+
Fragment,
4040
getParentComponent,
4141
getSelectedComponent,
4242
initializeIds,
4343
stateWithoutComponent,
4444
updatedState
4545
} from '@carbon-builder/sdk-react';
46+
import { useRemoteCustomComponentsCollections } from '../../utils/misc-tools';
4647

4748
const leftPaneWidth = '300px';
4849
const rightPaneWidth = '302px';
@@ -189,8 +190,10 @@ export const Edit = () => {
189190
addAction,
190191
undoAction,
191192
redoAction,
193+
customComponentsCollections,
192194
styleClasses
193195
} = useContext(GlobalStateContext);
196+
const [remoteCustomComponentsCollections] = useRemoteCustomComponentsCollections();
194197

195198
const params = useParams();
196199

@@ -309,8 +312,16 @@ export const Edit = () => {
309312
className={cx('edit-content', selectedLeftPane !== SelectedLeftPane.NONE ? 'is-side-panel-active' : '')}
310313
onClick={() => updateFragment({ ...fragment, selectedComponentId: null }, false)}>
311314
{
312-
// eslint-disable-next-line
313-
fragment && <Fragment fragment={fragment} setFragment={updateFragment} outline={fragment.outline} />
315+
fragment
316+
// eslint-disable-next-line react/jsx-no-useless-fragment
317+
&& <Fragment
318+
fragment={fragment}
319+
setFragment={updateFragment}
320+
outline={fragment.outline}
321+
fragments={fragments}
322+
customComponentsCollections={customComponentsCollections}
323+
styleClasses={styleClasses}
324+
remoteCustomComponentsCollections={remoteCustomComponentsCollections} />
314325
}
315326
</div>
316327
<div className={rightPanel}>
File renamed without changes.
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, {
2-
useContext,
32
useEffect,
43
useRef,
54
useState
@@ -11,8 +10,6 @@ import parse, { attributesToProps, domToReact } from 'html-react-parser';
1110
import { throttle } from 'lodash';
1211
import axios from 'axios';
1312
import Handlebars from 'handlebars';
14-
import { getFragmentsFromLocalStorage } from '../utils/fragment-tools';
15-
import { GlobalStateContext } from '../context';
1613
import { getAllFragmentStyleClasses, styleObjectToString } from '@carbon-builder/player-react';
1714
import {
1815
AComponent,
@@ -23,9 +20,8 @@ import {
2320
stateWithoutComponent,
2421
updateComponentCounter,
2522
updatedState
26-
} from '@carbon-builder/sdk-react';
23+
} from '../../index';
2724
import './fragment-preview.scss';
28-
import { useRemoteCustomComponentsCollections } from '../utils/misc-tools';
2925

3026
const canvas = css`
3127
border: 2px solid #d8d8d8;
@@ -101,12 +97,18 @@ const fetchCSS = async (urls: string[]) => {
10197
}
10298
};
10399

104-
export const Fragment = ({ fragment, setFragment, outline }: any) => {
105-
const globalState = useContext(GlobalStateContext);
100+
export const Fragment = ({
101+
fragment,
102+
setFragment,
103+
fragments,
104+
outline,
105+
remoteCustomComponentsCollections,
106+
customComponentsCollections,
107+
styleClasses
108+
}: any) => {
106109
const [showDragOverIndicator, setShowDragOverIndicator] = useState(false);
107110
const [customComponentsStyles, setCustomComponentsStyles] = useState(css``);
108111
const [customComponentsStylesUrls, _setCustomComponentsStylesUrls] = useState<string[]>([]);
109-
const [remoteCustomComponentsCollections] = useRemoteCustomComponentsCollections();
110112
const [allCustomComponentsCollections, setAllCustomComponentsCollections] = useState([] as any[]);
111113
const containerRef = useRef(null as any);
112114
const holderRef = useRef(null as any);
@@ -139,9 +141,9 @@ export const Fragment = ({ fragment, setFragment, outline }: any) => {
139141
useEffect(() => {
140142
setAllCustomComponentsCollections([
141143
...(remoteCustomComponentsCollections as any[] || []).flat(),
142-
...globalState.customComponentsCollections
144+
...customComponentsCollections
143145
]);
144-
}, [remoteCustomComponentsCollections, globalState.customComponentsCollections]);
146+
}, [remoteCustomComponentsCollections, customComponentsCollections]);
145147

146148
const resize = throttle((e: any) => {
147149
const rect = containerRef.current.getBoundingClientRect();
@@ -167,10 +169,6 @@ export const Fragment = ({ fragment, setFragment, outline }: any) => {
167169
return <SkeletonPlaceholder />;
168170
}
169171

170-
// try to use the state but get the fragments from local storage if state is not available
171-
// localStorage info is used when rendering and can't be used for interaction
172-
const { fragments } = globalState || { fragments: getFragmentsFromLocalStorage() };
173-
174172
// initialize component counter
175173
updateComponentCounter(fragment.data);
176174

@@ -365,7 +363,7 @@ export const Fragment = ({ fragment, setFragment, outline }: any) => {
365363
return null;
366364
};
367365

368-
const styles = css`${getAllFragmentStyleClasses(fragment, fragments, globalState?.styleClasses).map((styleClass: any) => `.${styleClass.id} {
366+
const styles = css`${getAllFragmentStyleClasses(fragment, fragments, styleClasses).map((styleClass: any) => `.${styleClass.id} {
369367
${styleClass.content}
370368
}`)
371369
}`;

sdk/react/src/lib/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './actions-connector';
22
export * from './fragment-layout-widget';
33
export * from './fragment-preview';
44
export * from './custom-components-collection-editor';
5+
export * from './fragment';

0 commit comments

Comments
 (0)