Skip to content

Commit a06c5f8

Browse files
authored
Consolidate plugin creation to a new function (#340)
This is a future-looking change. For #290 I need a function that can setup the plugin UI for an arbitrary widget and button. This is something I anticipate exposing off of FlipbookCore (#338) once I get around to that
1 parent 4f32c34 commit a06c5f8

File tree

5 files changed

+90
-74
lines changed

5 files changed

+90
-74
lines changed

src/Plugin/PluginApp.story.luau

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ return {
1010
story = React.createElement(ContextProviders, {
1111
plugin = MockPlugin.new() :: any,
1212
}, {
13-
PluginApp = React.createElement(PluginApp, {
14-
plugin = plugin,
15-
}),
13+
PluginApp = React.createElement(PluginApp),
1614
}),
1715
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
local React = require("@pkg/React")
2+
local ReactRoblox = require("@pkg/ReactRoblox")
3+
4+
local ContextProviders = require("@root/Common/ContextProviders")
5+
local PluginApp = require("@root/Plugin/PluginApp")
6+
7+
local function createFlipbookPlugin(
8+
plugin: Plugin,
9+
widget: DockWidgetPluginGui,
10+
button: PluginToolbarButton
11+
): {
12+
mount: () -> (),
13+
unmount: () -> (),
14+
}
15+
local connections: { RBXScriptConnection } = {}
16+
local root = ReactRoblox.createRoot(widget)
17+
18+
local app = React.createElement(ContextProviders, {
19+
plugin = plugin,
20+
overlayGui = widget :: GuiBase2d,
21+
}, {
22+
PluginApp = React.createElement(PluginApp),
23+
})
24+
25+
local function unmount()
26+
root:unmount()
27+
end
28+
29+
local function mount()
30+
root:render(app)
31+
end
32+
33+
table.insert(
34+
connections,
35+
button.Click:Connect(function()
36+
widget.Enabled = not widget.Enabled
37+
end)
38+
)
39+
40+
table.insert(
41+
connections,
42+
widget:GetPropertyChangedSignal("Enabled"):Connect(function()
43+
button:SetActive(widget.Enabled)
44+
end)
45+
)
46+
47+
table.insert(
48+
connections,
49+
widget:GetPropertyChangedSignal("Enabled"):Connect(function()
50+
if widget.Enabled then
51+
root:render(app)
52+
else
53+
unmount()
54+
end
55+
end)
56+
)
57+
58+
if widget.Enabled then
59+
mount()
60+
end
61+
62+
local function destroy()
63+
unmount()
64+
for _, connection in connections do
65+
connection:Disconnect()
66+
end
67+
end
68+
69+
return {
70+
mount = mount,
71+
unmount = unmount,
72+
destroy = destroy,
73+
}
74+
end
75+
76+
return createFlipbookPlugin

src/Plugin/createToggleButton.luau

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/Plugin/createWidget.luau

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/init.server.luau

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,22 @@ if RunService:IsRunning() or not RunService:IsEdit() then
44
return
55
end
66

7-
local React = require("@pkg/React")
8-
local ReactRoblox = require("@pkg/ReactRoblox")
9-
10-
local ContextProviders = require("@root/Common/ContextProviders")
11-
local PluginApp = require("@root/Plugin/PluginApp")
12-
local createToggleButton = require("@root/Plugin/createToggleButton")
13-
local createWidget = require("@root/Plugin/createWidget")
7+
local constants = require("@root/constants")
8+
local createFlipbookPlugin = require("@root/Plugin/createFlipbookPlugin")
149

1510
local PLUGIN_NAME = "Flipbook" .. if _G.BUILD_CHANNEL == "development" then ` {_G.BUILD_HASH}` else ""
1611

1712
local toolbar = plugin:CreateToolbar(PLUGIN_NAME)
18-
local widget = createWidget(plugin, PLUGIN_NAME)
19-
local root = ReactRoblox.createRoot(widget)
20-
local disconnectButton = createToggleButton(toolbar, widget)
21-
22-
local app = React.createElement(ContextProviders, {
23-
plugin = plugin,
24-
}, {
25-
PluginApp = React.createElement(PluginApp),
26-
})
27-
28-
local widgetConn = widget:GetPropertyChangedSignal("Enabled"):Connect(function()
29-
if widget.Enabled then
30-
root:render(app)
31-
else
32-
root:unmount()
33-
end
34-
end)
35-
36-
if widget.Enabled then
37-
root:render(app)
38-
end
3913

40-
plugin.Unloading:Connect(function()
41-
disconnectButton()
42-
widgetConn:Disconnect()
14+
local info = DockWidgetPluginGuiInfo.new(Enum.InitialDockState.Top, true)
15+
16+
local widget = plugin:CreateDockWidgetPluginGui(PLUGIN_NAME, info)
17+
widget.Name = PLUGIN_NAME
18+
widget.Title = PLUGIN_NAME
19+
widget.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
20+
21+
local button = toolbar:CreateButton(PLUGIN_NAME, "Open story view", constants.FLIPBOOK_LOGO)
22+
23+
local flipbookPlugin = createFlipbookPlugin(plugin, widget, button)
4324

44-
root:unmount()
45-
end)
25+
plugin.Unloading:Connect(flipbookPlugin.unmount)

0 commit comments

Comments
 (0)