local D=game:GetService("\083\101\108\101\099\116\105\111\110")local o=script:FindFirstAncestor("\084\101\120\116\069\100\105\116\111\114")local m=require(o.Packages.Roact)local c=require(o.Packages.Rodux)local f=require(o.Packages.RoactRodux)local R=require(o.Packages.Maid)local K=require(o.Plugin.Reducer)local C=require(o.Plugin.Components.App)local B=require(o.Plugin.assets)local t=DockWidgetPluginGuiInfo.new(Enum.InitialDockState.Float,false,false,200,300,150,150)local n=plugin:CreateToolbar("\084\101\120\116 \069\100\105\116\111\114")local k=n:CreateButton("\084\101\120\116 \069\100\105\116\111\114","\079\112\101\110 \116\104\101 \116\101\120\116 \101\100\105\116\111\114",B["\108\111\103\111-\115\116\117\100\105\111"])local S={toggleOpen=plugin:CreatePluginAction("\084\101\120\116\069\100\105\116\111\114\084\111\103\103\108\101\079\112\101\110","\084\101\120\116 \069\100\105\116\111\114\058 \084\111\103\103\108\101 \079\112\101\110","\084\111\103\103\108\101\115 \116\104\101 \116\101\120\116 \101\100\105\116\111\114 \116\111 \111\112\101\110\047\099\108\111\115\101",B["\108\111\103\111-\115\116\117\100\105\111"],true)}local P=plugin:CreateDockWidgetPluginGui("\084\101\120\116\069\100\105\116\111\114\087\105\100\103\101\116",t)P.Name="\084\101\120\116\069\100\105\116\111\114"P.Title="\084\101\120\116 \069\100\105\116\111\114"P.ZIndexBehavior=Enum.ZIndexBehavior.Sibling k:SetActive(P.Enabled)local W=R.new()local H=c.Store.new(K,{})local N=m.createElement(f.StoreProvider,{store=H},{App=m.createElement(C,{pluginActions=S})})local U=m.mount(N,P,"\084\101\120\116\069\100\105\116\111\114")local function E()local o=D:Get()if#o>0 then local D=o[1]if D:IsA("\084\101\120\116\076\097\098\101\108")or D:IsA("\084\101\120\116\066\117\116\116\111\110")or D:IsA("\084\101\120\116\066\111\120")then H:dispatch({type="\115\101\116\084\101\120\116\073\116\101\109",item=D})else H:dispatch({type="\115\101\116\084\101\120\116\073\116\101\109";item=nil})end else H:dispatch({type="\115\101\116\084\101\120\116\073\116\101\109",item=nil})end end local function G()if P.Enabled then E()end end local function y()P.Enabled=not P.Enabled k:SetActive(P.Enabled)if P.Enabled then E()end end P:BindToClose(function()P.Enabled=false k:SetActive(false)end)W:GiveTask(k.Click:Connect(y))W:GiveTask(S.toggleOpen.Triggered:Connect(y))W:GiveTask(D.SelectionChanged:Connect(G))plugin.Unloading:Connect(function()m.unmount(U)W:DoCleaning()end)
local Selection = game:GetService("Selection")
local TextEditor = script:FindFirstAncestor("TextEditor")

local Roact = require(TextEditor.Packages.Roact)
local Rodux = require(TextEditor.Packages.Rodux)
local RoactRodux = require(TextEditor.Packages.RoactRodux)
local Maid = require(TextEditor.Packages.Maid)
local Reducer = require(TextEditor.Plugin.Reducer)
local App = require(TextEditor.Plugin.Components.App)
local assets = require(TextEditor.Plugin.assets)

local textEditorWidgetInfo = DockWidgetPluginGuiInfo.new(Enum.InitialDockState.Float, false, false, 200, 300, 150, 150)
local toolbar = plugin:CreateToolbar("Text Editor")
local textEditorButton = toolbar:CreateButton("Text Editor", "Open the text editor", assets["logo-studio"])
local textEditorActions = {
  toggleOpen = plugin:CreatePluginAction("TextEditorToggleOpen", "Text Editor: Toggle Open", "Toggles the text editor to open/close", assets["logo-studio"], true),

  -- Not using PluginActions in this case because
  -- 1) PluginActions aren't fired within a textbox
  -- 2) Cannot pre-bind plugin actions to specific keybinds, so users have to go within their settings to set them

  -- toggleBold = plugin:CreatePluginAction("TextEditorToggleBold", "Text Editor: Toggle Bold", "Toggles bold in the text editor", "rbxassetid://4459262762", true),
  -- toggleItalic = plugin:CreatePluginAction("TextEditorToggleItalic", "Text Editor: Toggle Italic", "Toggles italic in the text editor", "rbxassetid://4459262762", true),
  -- toggleUnderline = plugin:CreatePluginAction("TextEditorToggleUnderline", "Text Editor: Toggle Underline", "Toggles underline in the text editor", "rbxassetid://4459262762", true),
  -- toggleStrikethrough = plugin:CreatePluginAction("TextEditorToggleStrikethrough", "Text Editor: Toggle Strikethrough", "Toggles strikethrough in the text editor", "rbxassetid://4459262762", true),
}
local textEditorWidget = plugin:CreateDockWidgetPluginGui("TextEditorWidget", textEditorWidgetInfo)
textEditorWidget.Name = "TextEditor"
textEditorWidget.Title = "Text Editor"
textEditorWidget.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
textEditorButton:SetActive(textEditorWidget.Enabled)

local pluginMaid = Maid.new()
local store = Rodux.Store.new(Reducer, {})

local app = Roact.createElement(RoactRodux.StoreProvider, {
  store = store,
}, {
  App = Roact.createElement(App, {
    pluginActions = textEditorActions
  })
})

local roactTree = Roact.mount(app, textEditorWidget, "TextEditor")

local function getSelectedObject()
  local items = Selection:Get()
  if #items > 0 then
    local textItem = items[1]
    if textItem:IsA("TextLabel") or textItem:IsA("TextButton") or textItem:IsA("TextBox") then
      store:dispatch({ type = "setTextItem", item = textItem })
    else
      store:dispatch({ type = "setTextItem", item = nil })
    end
  else
    store:dispatch({ type = "setTextItem", item = nil })
  end
end

local function selectionChanged()
  if textEditorWidget.Enabled then
    getSelectedObject()
  end
end

local function onButtonClick()
  textEditorWidget.Enabled = not textEditorWidget.Enabled
  textEditorButton:SetActive(textEditorWidget.Enabled)
  if textEditorWidget.Enabled then
    getSelectedObject()
  end
end

textEditorWidget:BindToClose(function()
  textEditorWidget.Enabled = false
  textEditorButton:SetActive(false)
end)

pluginMaid:GiveTask(textEditorButton.Click:Connect(onButtonClick))
pluginMaid:GiveTask(textEditorActions.toggleOpen.Triggered:Connect(onButtonClick))
pluginMaid:GiveTask(Selection.SelectionChanged:Connect(selectionChanged))

plugin.Unloading:Connect(function()
  Roact.unmount(roactTree)
  pluginMaid:DoCleaning()
end)