-- Compiled with roblox-ts v1.2.7 local TS = _G[script] local Roact = TS.import(script, TS.getModule(script, "@rbxts", "roact").src) local ZirconIcon = TS.import(script, script.Parent, "Icon").default local ZrRichTextHighlighter = TS.import(script, TS.getModule(script, "@rbxts", "zirconium").out.Ast).ZrRichTextHighlighter local _ThemeContext = TS.import(script, script.Parent.Parent, "UIKit", "ThemeContext") local ThemeContext = _ThemeContext.default local convertColorObjectToHex = _ThemeContext.convertColorObjectToHex local Maid = TS.import(script, TS.getModule(script, "@rbxts", "maid").Maid) local UserInputService = game:GetService("UserInputService") --[[ * * A basic syntax text box ]] local ZirconSyntaxTextBox do ZirconSyntaxTextBox = Roact.Component:extend("ZirconSyntaxTextBox") function ZirconSyntaxTextBox:init(props) self.ref = Roact.createRef() self.maid = Maid.new() self.focusMaid = Maid.new() self.state = { source = props.Source, cursorPosition = 0, virtualCursorPosition = 0, } end function ZirconSyntaxTextBox:didMount() local textBox = self.ref:getValue() if textBox then self.maid:GiveTask(UserInputService.InputEnded:Connect(function(io, gameProcessed) if self.state.focused then if io.KeyCode == Enum.KeyCode.Up then local _result = self.props.OnHistoryTraversal if _result ~= nil then _result(-1) end elseif io.KeyCode == Enum.KeyCode.Down then local _result = self.props.OnHistoryTraversal if _result ~= nil then _result(1) end end end end)) end end function ZirconSyntaxTextBox:willUnmount() self.maid:DoCleaning() self.focusMaid:DoCleaning() end function ZirconSyntaxTextBox:didUpdate(prevProps) local textBox = self.ref:getValue() if prevProps.Focused ~= self.props.Focused and (self.props.AutoFocus and textBox) then if self.props.Focused then textBox:CaptureFocus() else textBox:ReleaseFocus() end end if self.props.Source ~= prevProps.Source then self:setState({ source = self.props.Source, }) task.defer(function() return self:setState({ cursorPosition = #self.props.Source + 1, }) end) end end function ZirconSyntaxTextBox:render() return Roact.createElement(ThemeContext.Consumer, { render = function(theme) local highlighter = ZrRichTextHighlighter.new(self.state.source, theme.SyntaxHighlighter and convertColorObjectToHex(theme.SyntaxHighlighter) or nil) local _attributes = {} local _condition = self.props.Size if _condition == nil then _condition = UDim2.new(1, 0, 1, 0) end _attributes.Size = _condition _attributes.Position = self.props.Position _attributes.BackgroundColor3 = theme.SecondaryBackgroundColor3 _attributes.BorderSizePixel = 0 local _children = { Roact.createElement("UIPadding", { PaddingLeft = UDim.new(0, 5), PaddingRight = UDim.new(0, 5), PaddingBottom = UDim.new(0, 5), PaddingTop = UDim.new(0, 5), }), Roact.createElement("TextBox", { [Roact.Ref] = self.ref, BackgroundTransparency = 1, Font = "Code", TextSize = 18, TextXAlignment = "Left", TextYAlignment = "Top", ClearTextOnFocus = true, PlaceholderColor3 = theme.SecondaryTextColor3, PlaceholderText = self.props.PlaceholderText, CursorPosition = self.state.cursorPosition, MultiLine = self.props.MultiLine, Size = UDim2.new(1, 0, 1, 0), Text = self.state.source, [Roact.Change.Text] = function(rbx) return self:setState({ source = (string.gsub(rbx.Text, "\t", " ")), }) end, [Roact.Change.CursorPosition] = function(rbx) return self:setState({ virtualCursorPosition = rbx.CursorPosition, }) end, TextTransparency = 0.75, [Roact.Event.Focused] = function(rbx) self:setState({ focused = true, source = "", }) self.focusMaid:GiveTask(UserInputService.InputBegan:Connect(function(io) if io.UserInputState == Enum.UserInputState.Begin and io.UserInputType == Enum.UserInputType.Keyboard then local _result = self.props.CancelKeyCodes if _result ~= nil then local _keyCode = io.KeyCode _result = table.find(_result, _keyCode) ~= nil end if _result then local _result_1 = self.props.OnCancel if _result_1 ~= nil then _result_1() end rbx:ReleaseFocus() rbx.Text = "" elseif io:IsModifierKeyDown(Enum.ModifierKey.Ctrl) then local _result_1 = self.props.OnControlKey if _result_1 ~= nil then _result_1(io.KeyCode, io) end end end end)) end, [Roact.Event.FocusLost] = function(textBox, enterPressed, inputThatCausedFocusLoss) if enterPressed and not self.props.MultiLine then local _result = self.props.OnEnterSubmit if _result ~= nil then _result(textBox.Text) end end self:setState({ focused = false, }) if enterPressed and self.props.RefocusOnSubmit then -- Needs to be deferred, otherwise roblox keeps the enter key there. task.defer(function() return textBox:CaptureFocus() end) end self.focusMaid:DoCleaning() end, [Roact.Event.InputChanged] = function(rbx, io) if io.UserInputType == Enum.UserInputType.Keyboard then local _result = self.props.CancelKeyCodes if _result ~= nil then local _keyCode = io.KeyCode _result = table.find(_result, _keyCode) ~= nil end if _result then rbx:ReleaseFocus() end end end, TextColor3 = theme.PrimaryTextColor3, }), Roact.createElement("TextLabel", { TextXAlignment = "Left", TextYAlignment = "Top", Font = "Code", Size = UDim2.new(1, 0, 1, 0), TextSize = 18, RichText = true, BackgroundTransparency = 1, Text = highlighter:parse(), TextColor3 = Color3.fromRGB(198, 204, 215), }), } local _length = #_children local _child = self.state.source ~= "" and (Roact.createElement("TextButton", { BackgroundTransparency = 1, Text = "", Size = UDim2.new(0, 20, 0, 20), Position = UDim2.new(1, -25, 0, 0), [Roact.Event.MouseButton1Click] = function() return self:setState({ source = "", }) end, }, { Roact.createElement("UIListLayout", { VerticalAlignment = "Center", HorizontalAlignment = "Center", }), Roact.createElement(ZirconIcon, { Icon = "Close", }), })) if _child then if _child.elements ~= nil or _child.props ~= nil and _child.component ~= nil then _children[_length + 1] = _child else for _k, _v in ipairs(_child) do _children[_length + _k] = _v end end end return Roact.createElement("Frame", _attributes, _children) end, }) end end return { default = ZirconSyntaxTextBox, }