/** * Component for cluebox input, author only. * * @packageDocumentation * @module crossword * @mergeModuleWith webwriter-word-puzzles */ import { html, render, LitElement } from 'lit'; import { customElement, property, query } from 'lit/decorators.js'; import { WebwriterWordPuzzle } from './webwriter-word-puzzle'; import { WordClue } from '../lib/crossword-gen'; import { cluebox_styles } from '../styles/styles' // Shoelace import "@shoelace-style/shoelace/dist/themes/light.css"; import { SlButton, SlAlert, SlDrawer } from '@shoelace-style/shoelace'; import SlIcon from "@shoelace-style/shoelace/dist/components/icon/icon.component.js"; // Icons import plus from 'bootstrap-icons/icons/plus-lg.svg'; import minus from 'bootstrap-icons/icons/dash.svg'; import magic_wand from 'bootstrap-icons/icons/magic.svg'; import pencil_square from 'bootstrap-icons/icons/pencil-square.svg'; import {localized, msg} from "@lit/localize" import LOCALIZE from "../../localization/generated" /** * Crossword element for word puzzle widget. Includes grid and clue panel elements. * @returns { void } Nothing, but renders the DOM element for the crossword puzzle */ @localized() @customElement("webwriter-word-puzzle-cluebox-input") export class WebwriterWordPuzzleClueboxInput extends LitElement { // All methods have the same names as in crosswords-js public localize = LOCALIZE /** * Whether the current display is a preview */ @property({ type: Boolean, state: true, attribute: false }) _preview: boolean = false /** * The input element for the words and clues of the crossword puzzle. * * It's intended exclusively for use by crossword creators (i.e. teachers). * * See the constructor {@link WebwriterWordPuzzle.newClueBox | newClueBox()} */ @query(".clueboxInput") accessor clueboxInput: HTMLTableElement /** * The list of words grouped with their clues, direction, and word number. */ @property({type: Array, attribute: false}) _wordsClues: WordClue[] = [{word: "", across: true}] /** * drawer */ @query("sl-drawer") accessor drawer: SlDrawer /** * @constructor * * Does nothing I guess */ constructor(private parentComponent: WebwriterWordPuzzle) { super() } static get styles() { return cluebox_styles } /** * Dispatches an event to update the current words and clues. * * @param {number} clue the updated clue number */ setWordsClues(wordsClues: WordClue[]): void { let setWordsClues = new CustomEvent("set-words-clues", {bubbles: true, composed: true, detail: wordsClues}) this.dispatchEvent(setWordsClues) } /** * Event handler that triggers crossword generation */ triggerCwGeneration() { this.getNewWords() if(this._wordsClues.length != 0) { const genClicked = new CustomEvent("generateCw", {bubbles: true, composed: true}) this.dispatchEvent(genClicked) } } /** * Extracts the words from the cluebox. * Calls {@link setWordsClues} * */ getNewWords() { let wordsAndClues = [] const rows = this.clueboxInput.querySelectorAll("tbody tr") let words: string[] = Array.from(rows).map(row => row.querySelector("td")?.textContent?.trim() || null ) // Hard-coded the row here let clues: string[] = Array.from(rows).map(row => row.querySelectorAll("td")[2]?.textContent?.trim() || null ) for (let i = 0; i < words.length; i++) { if(words[i] != null) { wordsAndClues.push({word: words[i], clueText: clues[i]}) } } this.setWordsClues(wordsAndClues) return this._wordsClues } showDrawer() { this.drawer.show() this.clueboxInput.tBodies[0].rows[0].cells[0].focus() } hideDrawer() { this.drawer.hide() } drawerKeyHandler(event: KeyboardEvent): void { //DEV: console.log("Drawer handler") this.ctrlHandler(event) if (event.key === "Escape") { event.stopPropagation() this.hideDrawer() } } /** Event handler for stopping control propagation and rendering * */ ctrlHandler(event: KeyboardEvent): void { if (event.ctrlKey && event.key === "Enter") { event.stopPropagation() this.getNewWords() if(this._wordsClues.length != 0) { this.triggerCwGeneration() } } else if(event.ctrlKey) event.stopPropagation() } /** * Handler for deleting the row corresponding to the clicked button. * * @param {Event} e Click event of the button */ deleteRow(e: Event) { let button: HTMLButtonElement = (e.target) const trow: HTMLTableRowElement = button.closest("tr") const tBody: HTMLTableRowElement = trow.closest("tBody") if(tBody.childElementCount > 5) { trow.remove() }else { // Clear content if four or less rows remaining const [td1, _, td2] = trow.querySelectorAll("td"); td1.textContent = ""; td2.textContent = ""; } } /** * Handler for deleting the row corresponding to the clicked button. * * @param {Event} e Click event of the button */ addRow(e: Event) { let newRow = this.clueboxInput.tBodies[0].insertRow() render(this.new_row_template_inner(), newRow) } /** * Lit HTML template for adding a new row to cluebox input element. * Used in {@link WwWordPuzzlesCwCluebox.addRow() | addRow()} */ new_row_template_inner() { return html` ${this.parentComponent.type == "crossword" ? html` ` : html``}
this.deleteRow(e)}>
` } onPreviewToggle(contenteditable: boolean): void { //DEV: console.log("Preview processing for crossword-cluebox") this._preview = !contenteditable //DEV: console.log("this._preview:") //DEV: console.log(this._preview) this.requestUpdate() } renderClueboxInput() { //DEV: console.log("render cluebox input") const clueboxInputRender = [] const clueboxButtonCell = html`
this.deleteRow(e)}>
` if(this._wordsClues != null) { let i = 0 for(i < 0; i < this._wordsClues.length; i++) { if(this._wordsClues[i].word != "") { if(this.parentComponent.type == "crossword") { clueboxInputRender.push(this._wordsClues[i].clueText != "" ? html`${this._wordsClues[i].word} ${this._wordsClues[i].clueText}${clueboxButtonCell}` : html`${this._wordsClues[i].word} ${clueboxButtonCell}` ) }else { clueboxInputRender.push(html`${this._wordsClues[i].word}${clueboxButtonCell}`) } } else { clueboxInputRender.push(html`${this.new_row_template_inner()}`) } } // Always have at least 5 rows present if(i < 5) { let empty = 5 - i for(empty; empty > 0; empty--) { clueboxInputRender.push(html`${this.new_row_template_inner()}`) } } } else { for(let i = 0; i < 5; i++) { clueboxInputRender.push(html`${this.new_row_template_inner()}`) } } const wordColumnWidthStyle = this.parentComponent.type == "crossword" ? "width: 30%" : "width: 100%" /** * cluebox template */ return html` ${this.parentComponent.type == "crossword" ? html`` : html``} ${this.parentComponent.type == "crossword" ? html`` : html``} ${clueboxInputRender}
${msg("Words")}
this.addRow(e)}>
${msg("Clues")}
` } reloadUnplacedMarkers(wordsClues: WordClue[]) { const tbody = this.renderRoot?.querySelector('tbody'); if (!tbody) return; const rows = Array.from(tbody.querySelectorAll('tr')); for (const row of rows) { const firstCell = row.querySelector('td'); const cellText = firstCell?.textContent?.trim(); if (wordsClues.find((wc) => wc.word == cellText && wc.x == null)) { row.classList.add('cell-word-not-placed'); } else { row.classList.remove('cell-word-not-placed'); } } } render() { /** * clueboxInput template */ //DEV: console.log("parent has attr contenteditable: " + this._parent.hasAttribute("contenteditable")) //this.onPreviewToggle(this._parent.hasAttribute("contenteditable")) const sl_drawer = html` ${this.renderClueboxInput()} {this.triggerCwGeneration();}}> ${msg("Generate puzzle")} ` return html`${!this._preview ? sl_drawer : html``}` } }