Seite 1 von 1

Editorgadget Cursor stelle auslesen

Verfasst: 20.10.2022 10:01
von stevie1401
Ich möchte gerne in einem Edsitorgadget irgendwo hinklicken und dort dann etwas einfügen (Zum beispiel Smileys oder andere Bilder). Dazu brauche ich allerdings die Info wo sich der Cusor befindet.
Gibt es eine Möglichkeit in Spiderbasic oder Javascript das herauszufinden?

Re: Editorgadget Cursor stelle auslesen

Verfasst: 20.10.2022 15:57
von Kiffi

Code: Alles auswählen

; https://stackoverflow.com/questions/11076975/how-To-insert-text-into-the-textarea-at-the-current-cursor-position

EnableExplicit

Enumeration
  #Window
  #Editor
  #Button
EndEnumeration

Procedure InsertTextAtCurrentPosition(Editor, TextToInsert.s)
  
  If IsGadget(Editor) = #False : ProcedureReturn : EndIf
  If GadgetType(Editor) <> #PB_GadgetType_Editor : ProcedureReturn : EndIf
  
  Protected GID = GadgetID(Editor)
  
  ! var cursorPos = $(v_gid.gadget.textbox).prop('selectionStart');
  ! var v = $(v_gid.gadget.textbox).val();
  ! var textBefore = v.substring(0,  cursorPos);
  ! var textAfter  = v.substring(cursorPos, v.length);
  ! 
  ! $(v_gid.gadget.textbox).val(textBefore + v_texttoinsert + textAfter);
  ! $(v_gid.gadget.textbox).prop({ 'selectionStart': cursorPos + v_texttoinsert.length, 'selectionEnd': cursorPos + v_texttoinsert.length });
  
  SetActiveGadget(Editor)
  
EndProcedure

Procedure ButtonGadgetEvent()
  
  InsertTextAtCurrentPosition(#Editor, " Hello! ")
  
EndProcedure

OpenWindow(#Window, 0, 0, 400, 220, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(#Editor, 10, 10, 380, 150)
SetGadgetText(#Editor, "This is a multiline text to edit" + #LF$ + "in the EditorGadget()." + #LF$ + "Place the cursor inside this text" + #LF$ + "and press the Button.")
ButtonGadget(#Button, (400-100)/2, 170, 100, 40, "Insert")
BindGadgetEvent(#Button, @ButtonGadgetEvent())

Re: Editorgadget Cursor stelle auslesen

Verfasst: 20.10.2022 17:10
von stevie1401
Genial! :)