-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add basic sheet rendering support * chore: add missing file * fix: correct bg colors for none support * chore: fix test that relied on dark bg * chore: fix ref * chore: api spec update * chore: add todo comments * chore: use memo * chore: a bit less verbose * chore: list * chore: cleaning * chore: add rendering test * chore: enable rendering test * chore: settings * chore: settings * chore: disable rendering tests * chore: revert test tests
- Loading branch information
Showing
26 changed files
with
689 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
*.rej | ||
*.tmp | ||
*.log | ||
*.pem | ||
.cache/ | ||
.DS_Store | ||
.idea/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import React, { useEffect, useState, useContext, useMemo } from 'react'; | ||
import useLayout from '../hooks/useLayout'; | ||
import getObject from '../object/get-object'; | ||
import Cell from './Cell'; | ||
import uid from '../object/uid'; | ||
import { resolveBgColor, resolveBgImage } from '../utils/background-props'; | ||
import InstanceContext from '../contexts/InstanceContext'; | ||
|
||
/** | ||
* @interface | ||
* @extends HTMLElement | ||
* @experimental | ||
* @since 3.1.0 | ||
*/ | ||
const SheetElement = { | ||
/** @type {'njs-sheet'} */ | ||
className: 'njs-sheet', | ||
}; | ||
|
||
function getCellRenderer(cell, halo, initialSnOptions, initialSnPlugins, initialError, onMount) { | ||
const { x, y, width, height } = cell.bounds; | ||
return ( | ||
<div style={{ left: `${x}%`, top: `${y}%`, width: `${width}%`, height: `${height}%`, position: 'absolute' }}> | ||
<Cell | ||
ref={cell.cellRef} | ||
halo={halo} | ||
model={cell.model} | ||
currentId={cell.currentId} | ||
initialSnOptions={initialSnOptions} | ||
initialSnPlugins={initialSnPlugins} | ||
initialError={initialError} | ||
onMount={onMount} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
function Sheet({ model, halo, initialSnOptions, initialSnPlugins, initialError, onMount }) { | ||
const { root } = halo; | ||
const [layout] = useLayout(model); | ||
const { theme: themeName } = useContext(InstanceContext); | ||
const [cells, setCells] = useState([]); | ||
const [bgColor, setBgColor] = useState(undefined); | ||
const [bgImage, setBgImage] = useState(undefined); | ||
const [deepHash, setDeepHash] = useState(''); | ||
|
||
/// For each object | ||
useEffect(() => { | ||
if (layout) { | ||
const hash = JSON.stringify(layout.cells); | ||
if (hash === deepHash) { | ||
return; | ||
} | ||
setDeepHash(hash); | ||
const fetchObjects = async () => { | ||
/* | ||
Need to always fetch and evaluate everything as the sheet need to support multiple instances of the same object? | ||
No, there is no way to add the same chart twice, so the optimization should be worth it. | ||
*/ | ||
|
||
// Clear the cell list | ||
cells.forEach((c) => { | ||
root.removeCell(c.currentId); | ||
}); | ||
|
||
const lCells = layout.cells; | ||
// TODO - should try reuse existing objects on subsequent renders | ||
// Non-id updates should only change the "css" | ||
const cs = await Promise.all( | ||
lCells.map(async (c) => { | ||
let mounted; | ||
const mountedPromise = new Promise((resolve) => { | ||
mounted = resolve; | ||
}); | ||
|
||
const cell = cells.find((ce) => ce.id === c.name); | ||
if (cell) { | ||
cell.bounds = c.bounds; | ||
delete cell.mountedPromise; | ||
return cell; | ||
} | ||
const vs = await getObject({ id: c.name }, halo); | ||
return { | ||
model: vs.model, | ||
id: c.name, | ||
bounds: c.bounds, | ||
cellRef: React.createRef(), | ||
currentId: uid(), | ||
mounted, | ||
mountedPromise, | ||
}; | ||
}) | ||
); | ||
cs.forEach((c) => root.addCell(c.currentId, c.cellRef)); | ||
setCells(cs); | ||
}; | ||
fetchObjects(); | ||
} | ||
}, [layout]); | ||
|
||
const cellRenderers = useMemo( | ||
() => | ||
cells | ||
? cells.map((c) => getCellRenderer(c, halo, initialSnOptions, initialSnPlugins, initialError, c.mounted)) | ||
: [], | ||
[cells] | ||
); | ||
|
||
useEffect(() => { | ||
const bgComp = layout?.components ? layout.components.find((comp) => comp.key === 'general') : null; | ||
setBgColor(resolveBgColor(bgComp, halo.public.theme)); | ||
setBgImage(resolveBgImage(bgComp, halo.app)); | ||
}, [layout, halo.public.theme, halo.app, themeName]); | ||
|
||
/* TODO | ||
- sheet title + bg + logo etc + as option | ||
- sheet exposed classnames for theming | ||
*/ | ||
|
||
const height = !layout || Number.isNaN(layout.height) ? '100%' : `${Number(layout.height)}%`; | ||
const promises = cells.map((c) => c.mountedPromise); | ||
const ps = promises.filter((p) => !!p); | ||
if (ps.length) { | ||
Promise.all(promises).then(() => { | ||
// TODO - correct? Currently called each time a new cell is mounted? | ||
onMount(); | ||
}); | ||
} | ||
return ( | ||
<div | ||
className={SheetElement.className} | ||
style={{ | ||
width: `100%`, | ||
height, | ||
position: 'relative', | ||
backgroundColor: bgColor, | ||
backgroundImage: bgImage && bgImage.url ? `url(${bgImage.url})` : undefined, | ||
backgroundRepeat: 'no-repeat', | ||
backgroundSize: bgImage && bgImage.size, | ||
backgroundPosition: bgImage && bgImage.pos, | ||
}} | ||
> | ||
{cellRenderers} | ||
</div> | ||
); | ||
} | ||
|
||
export default Sheet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import Sheet from './Sheet'; | ||
|
||
export default function glue({ halo, element, model, initialSnOptions, initialSnPlugins, onMount, initialError }) { | ||
const { root } = halo; | ||
const sheetRef = React.createRef(); | ||
const portal = ReactDOM.createPortal( | ||
<Sheet | ||
ref={sheetRef} | ||
halo={halo} | ||
model={model} | ||
initialSnOptions={initialSnOptions} | ||
initialSnPlugins={initialSnPlugins} | ||
initialError={initialError} | ||
onMount={onMount} | ||
/>, | ||
element, | ||
model.id | ||
); | ||
|
||
const unmount = () => { | ||
root.remove(portal); | ||
model.removeListener('closed', unmount); | ||
}; | ||
|
||
model.on('closed', unmount); | ||
|
||
root.add(portal); | ||
return [unmount, sheetRef]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import init from './initiate'; | ||
import initSheet from './initiate-sheet'; | ||
import { modelStore, rpcRequestModelStore } from '../stores/model-store'; | ||
|
||
/** | ||
* @interface BaseConfig | ||
* @description Basic rendering configuration for rendering an object | ||
* @property {HTMLElement} element | ||
* @property {object=} options | ||
* @property {Plugin[]} [plugins] | ||
*/ | ||
|
||
/** | ||
* @interface GetConfig | ||
* @description Rendering configuration for rendering an existing object | ||
* @extends BaseConfig | ||
* @property {string} id | ||
*/ | ||
|
||
export default async function getObject({ id, options, plugins, element }, halo) { | ||
const key = `${id}`; | ||
let rpc = rpcRequestModelStore.get(key); | ||
if (!rpc) { | ||
rpc = halo.app.getObject(id); | ||
rpcRequestModelStore.set(key, rpc); | ||
} | ||
const model = await rpc; | ||
modelStore.set(key, model); | ||
|
||
if (model.genericType === 'sheet') { | ||
return initSheet(model, { options, plugins, element }, halo); | ||
} | ||
|
||
return init(model, { options, plugins, element }, halo); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* eslint no-underscore-dangle:0 */ | ||
import sheetAPI from '../sheet'; | ||
|
||
export default async function initSheet(model, optional, halo, initialError, onDestroy = async () => {}) { | ||
const api = sheetAPI({ | ||
model, | ||
halo, | ||
initialError, | ||
onDestroy, | ||
}); | ||
|
||
if (optional.options) { | ||
api.__DO_NOT_USE__.options(optional.options); | ||
} | ||
if (optional.plugins) { | ||
api.__DO_NOT_USE__.plugins(optional.plugins); | ||
} | ||
if (optional.element) { | ||
await api.__DO_NOT_USE__.mount(optional.element); | ||
} | ||
|
||
return api; | ||
} |
Oops, something went wrong.