Skip to content

Commit

Permalink
basic integrator loop to update a graph sorting texts continuously
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Werner committed Oct 27, 2023
1 parent b8002c6 commit 05957d1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 82 deletions.
10 changes: 9 additions & 1 deletion web/src/lib/useMuuriGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function isFullTriangle(grid) {
}
const useMuuriGrid = (gridRef, options, addInstance, removeInstance, size) => {
useEffect(() => {
try {
if (!gridRef.current) return

const grid = new Muuri(gridRef.current, options)
Expand Down Expand Up @@ -54,7 +55,14 @@ const useMuuriGrid = (gridRef, options, addInstance, removeInstance, size) => {
// Remove event listener for dragReleaseEnd event
grid.off('dragReleaseEnd')
removeInstance(grid)
grid.destroy()
try {
grid.destroy()
} catch (e) {
console.log(e)
}
}
} catch (e) {
console.log(e)
}
}, [gridRef, options, addInstance, removeInstance])
}
Expand Down
117 changes: 36 additions & 81 deletions web/src/ui/Puzzle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,7 @@ import useMuuriGrid from '../lib/useMuuriGrid'
import jsonPatch from 'fast-json-patch'

const SIZE = 300
const getPosition = (key, size, nest) => {
""
"Calculate the position of the triangle based on its key."
""
let x = 0
let y = 0
if (key === 1) {
x = 0.5 * size * (1 / nest)
y = 0
}
if (key === 2) {

x = 0
y = size * (1 / nest)
}
if (key === 3) {

x = size * (1 / nest)
y = size * (1 / nest)
}

return [x, y]
}

const moptions = (size, nest, all_muris) => ({
dragEnabled: true,
layout: function (grid, layoutId, items, width, height, callback) {
Expand Down Expand Up @@ -90,10 +68,16 @@ const moptions = (size, nest, all_muris) => ({
},
dragContainer: document.body,
dragStartPredicate: (item, event) => {
event.srcEvent.stopPropagation()
return Muuri.ItemDrag.defaultStartPredicate(item, event)
try {
event.srcEvent.stopPropagation()
return Muuri.ItemDrag.defaultStartPredicate(item, event)
} catch (e) {
console.log('dragStartPredicate', e)
}

},
dragSort: () => {

console.log('dragSort', all_muris)
return all_muris
},
Expand Down Expand Up @@ -146,20 +130,28 @@ const MutableTriangle = ({ nest, fullId, data, _key, size }) => {
<div className="puzzle-item-content triangle-content"></div>
{!isLeafNode && data && (
<div ref={gridRef} className="puzzle-grid">
{Object.entries(data).sort(([a], [b]) => b - a

).map(
([key, item]) =>
data && key !== "." &&(
<MutableTriangle
{<MutableTriangle
nest={nest + 1}
fullId={fullId + "3"}
data={data[3] ?? {".": ""} }
_key={fullId + "3"}
size={size / 2}
/> }
{<MutableTriangle
nest={nest + 1}
fullId={fullId + "2"}
data={data[2] ?? {".": ""}}
_key={fullId + "2"}
size={size / 2}
/> }
{<MutableTriangle
nest={nest + 1}
fullId={fullId + _key}
data={item}
_key={key}
fullId={fullId + "1"}
data={data[1] ?? {".": ""}}
_key={fullId + "1"}
size={size / 2}
/>
),
)}
/> }

</div>
)}
</div>
Expand All @@ -175,52 +167,15 @@ export const Puzzle = ({
const gridRef = useRef(null)
const { addInstance, removeInstance, muuriInstances } = useMuuriStore()

const [items, _setItems] = useState(
data ?? {
1: {
'.': 'Be.md',
},
2: {
'.': 'Nothing.md',
},
3: {
1: {
'.': 'Transition.md',
},
2: {
'.': 'Coming-into-Being and Ceasing-to-Be.md',
},
3: {
'.': 'Sublation.md',

1: {
'.': 'Becoming.md',
},
2: {
'.': 'Sameness-Different.md',
},
3: {
'.': 'Contradiction.md',
},
},

'.': 'Becoming.md',
},
'.': 'Ontology.md',
}
)

const setItems = (items) => {
_setItems(items)
const patch = jsonPatch.compare(data, items)
applyPatch(patch)
}
const [items, setItems] = useState(JSON.parse(JSON.stringify(data)))

useEffect(() => {
if (data) {
_setItems(data)
}
}, [data])
setItems(JSON.parse(JSON.stringify(data)))
}, [data]);





useMuuriGrid(
gridRef,
Expand Down

0 comments on commit 05957d1

Please sign in to comment.