This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extend plugin API supports move widget (#346)
* feat: add plugin api set widget position * refactor: add missing set state * refactor: set state * refactor: async insert * refactor: clean up * refactor: type definition & naming * refactor: separate hooks * refactor: clean up & renaming
- Loading branch information
Showing
8 changed files
with
139 additions
and
2 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
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
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
101 changes: 101 additions & 0 deletions
101
src/components/molecules/Visualizer/useWidgetAlignSystem.ts
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,101 @@ | ||
import { useEffect, useState, useCallback } from "react"; | ||
|
||
import type { WidgetLocationOptions } from "./Plugin/types"; | ||
import type { | ||
Widget, | ||
WidgetAlignSystem, | ||
WidgetSection, | ||
WidgetZone, | ||
WidgetArea, | ||
} from "./WidgetAlignSystem"; | ||
|
||
export default ({ alignSystem }: { alignSystem: WidgetAlignSystem | undefined }) => { | ||
const [overriddenAlignSystem, setOverrideAlignSystem] = useState<WidgetAlignSystem | undefined>( | ||
alignSystem, | ||
); | ||
|
||
const moveWidget = useCallback((widgetId: string, options: WidgetLocationOptions) => { | ||
if ( | ||
!["outer", "inner"].includes(options.zone) || | ||
!["left", "center", "right"].includes(options.section) || | ||
!["top", "middle", "bottom"].includes(options.area) || | ||
(options.section === "center" && options.area === "middle") | ||
) | ||
return; | ||
|
||
let widget: Widget | undefined; | ||
|
||
setOverrideAlignSystem(alignSystem => { | ||
if (!alignSystem) return alignSystem; | ||
Object.keys(alignSystem).forEach(zoneName => { | ||
const zone = alignSystem[zoneName as keyof WidgetAlignSystem]; | ||
if (zone) { | ||
Object.keys(zone).forEach(sectionName => { | ||
const section = zone[sectionName as keyof WidgetZone]; | ||
if (section) { | ||
Object.keys(section).forEach(areaName => { | ||
const area = section[areaName as keyof WidgetSection]; | ||
if (!widget && area?.widgets) { | ||
const sourceIndex = area.widgets.findIndex(w => w.id === widgetId); | ||
if (sourceIndex !== -1) { | ||
[widget] = area.widgets.splice(sourceIndex, 1); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
return { ...alignSystem }; | ||
}); | ||
|
||
setTimeout(() => { | ||
setOverrideAlignSystem(alignSystem => { | ||
if (!alignSystem || !widget) return alignSystem; | ||
if (!alignSystem[options.zone]) { | ||
alignSystem[options.zone] = { | ||
left: undefined, | ||
center: undefined, | ||
right: undefined, | ||
}; | ||
} | ||
|
||
const targetZone = alignSystem[options.zone] as WidgetZone; | ||
if (!targetZone[options.section]) { | ||
targetZone[options.section] = { | ||
top: undefined, | ||
middle: undefined, | ||
bottom: undefined, | ||
}; | ||
} | ||
|
||
const targetSection = targetZone[options.section] as WidgetSection; | ||
if (!targetSection[options.area]) { | ||
targetSection[options.area] = { | ||
align: "start", | ||
widgets: [], | ||
}; | ||
} | ||
|
||
const targetArea = targetSection[options.area] as WidgetArea; | ||
if (!targetArea.widgets) targetArea.widgets = []; | ||
if (options.method === "insert") { | ||
targetArea.widgets.unshift(widget); | ||
} else { | ||
targetArea.widgets.push(widget); | ||
} | ||
|
||
return { ...alignSystem }; | ||
}); | ||
}, 0); | ||
}, []); | ||
|
||
useEffect(() => { | ||
setOverrideAlignSystem(alignSystem); | ||
}, [alignSystem]); | ||
|
||
return { | ||
overriddenAlignSystem, | ||
moveWidget, | ||
}; | ||
}; |