/** * 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`
| ${msg("Words")} | ${this.parentComponent.type == "crossword" ? html` | ${msg("Clues")} | ` : html``}
|---|