Skip to content

Commit d4f8758

Browse files
committed
Added paste
1 parent f7c4165 commit d4f8758

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/elements/Input.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Input.defineProperty(Input, "replaceChar", {default = nil, type = "string", canT
3434
Input.defineEvent(Input, "mouse_click")
3535
Input.defineEvent(Input, "key")
3636
Input.defineEvent(Input, "char")
37+
Input.defineEvent(Input, "paste")
3738

3839
--- @shortDescription Creates a new Input instance
3940
--- @return Input object The newly created Input instance
@@ -192,6 +193,24 @@ function Input:blur()
192193
self:updateRender()
193194
end
194195

196+
function Input:paste(content)
197+
if not self.get("focused") then return false end
198+
local text = self.get("text")
199+
local pos = self.get("cursorPos")
200+
local maxLength = self.get("maxLength")
201+
local pattern = self.get("pattern")
202+
local newText = text:sub(1, pos - 1) .. content .. text:sub(pos)
203+
if maxLength and #newText > maxLength then
204+
newText = newText:sub(1, maxLength)
205+
end
206+
if pattern and not newText:match(pattern) then
207+
return false
208+
end
209+
self.set("text", newText)
210+
self.set("cursorPos", pos + #content)
211+
self:updateViewport()
212+
end
213+
195214
--- @shortDescription Renders the input element
196215
--- @protected
197216
function Input:render()

src/elements/TextBox.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ TextBox.defineEvent(TextBox, "mouse_click")
2929
TextBox.defineEvent(TextBox, "key")
3030
TextBox.defineEvent(TextBox, "char")
3131
TextBox.defineEvent(TextBox, "mouse_scroll")
32+
TextBox.defineEvent(TextBox, "paste")
3233

3334
--- Creates a new TextBox instance
3435
--- @shortDescription Creates a new TextBox instance
@@ -235,6 +236,23 @@ function TextBox:mouse_click(button, x, y)
235236
return false
236237
end
237238

239+
function TextBox:paste(text)
240+
if not self.get("editable") or not self.get("focused") then return false end
241+
local lines = self.get("lines")
242+
local cursorX = self.get("cursorX")
243+
local cursorY = self.get("cursorY")
244+
245+
for char in text:gmatch(".") do
246+
if char == "\n" then
247+
newLine(self)
248+
else
249+
insertChar(self, char)
250+
end
251+
end
252+
253+
return true
254+
end
255+
238256
--- Sets the text of the TextBox
239257
--- @shortDescription Sets the text of the TextBox
240258
--- @param text string The text to set

0 commit comments

Comments
 (0)