import "@hotwired/turbo-rails"
import "controllers"
import Trix from "trix"

// Bind ctrl + s to submit form
document.addEventListener('keydown', function (event) {
  // Check if Ctrl key is pressed and the pressed key is 'S'
  if ((event.ctrlKey || event.metaKey) && event.key === 's') {
    // Prevent the browser's default save action
    event.preventDefault();

    // Submit the form
    const form = document.querySelector('.has--key-ctrls');
    if (form) {
      form.requestSubmit();
    }
  }
});

// Trix
Trix.config.blockAttributes.heading2 = { tagName: "h2", terminal: true, breakOnReturn: true, group: false }
// Trix.config.blockAttributes.heading3 = { tagName: "h3", terminal: true, breakOnReturn: true, group: false }


document.addEventListener("trix-initialize", event => {
  var buttonHTML

  buttonHTML = '<button type="button" class="trix-button trix-button--icon trix-button--icon-heading-2" data-trix-attribute="heading2">Heading</button>';

  const groupElement = event.target.toolbarElement.querySelector(".trix-button-group--block-tools .trix-button--icon-heading-1")
	groupElement.insertAdjacentHTML("afterend", buttonHTML)

  const { toolbarElement } = event.target
  const inputElement = toolbarElement.querySelector("input[name=href]")
  inputElement.type = "text"
  inputElement.pattern = "(https?://|/).+"
})

