image-insert.js 835 B

1234567891011121314151617181920212223242526272829
  1. function insertHtmlAtCursor(textarea, html) {
  2. const start = textarea.selectionStart;
  3. const end = textarea.selectionEnd;
  4. const before = textarea.value.substring(0, start);
  5. const after = textarea.value.substring(end);
  6. textarea.value = before + html + after;
  7. // move cursor after inserted block
  8. textarea.selectionStart = textarea.selectionEnd = start + html.length;
  9. }
  10. function insertSelectedImage() {
  11. const select = document.getElementById("imageSelect");
  12. const imageId = select.value;
  13. if (!imageId) {
  14. alert("Please select an image first.");
  15. return;
  16. }
  17. const html = `<div class="text-center">
  18. <img src="/api/v1/images/${imageId}" class="rounded img-fluid">
  19. </div>`;
  20. const textarea = document.getElementById("myTextarea");
  21. insertHtmlAtCursor(textarea, html);
  22. }