Skip to content

Commit

Permalink
refactor(ts): readingmode
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Nov 7, 2024
1 parent 651bb4a commit d809a1f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,56 @@
/** @format */
import angular from "angular"
import {
TextReaderDataContainer,
TextReaderTokenContainer,
TextReaderWordHandler,
} from "@/controllers/text_reader_controller"
import angular, { IController, IRootElementService } from "angular"

type StandardReadingModeController = IController & {
data: TextReaderDataContainer
wordClick: TextReaderWordHandler
}

angular.module("korpApp").component("standardReadingMode", {
bindings: {
data: "<",
wordClick: "&",
wordClick: "<",
},
controller: [
"$element",
function ($element) {
const ctrl = this
function ($element: IRootElementService) {
const ctrl = this as StandardReadingModeController

function standardInnerElem(document) {
const doc = []
function standardInnerElem(document: TextReaderTokenContainer) {
const doc: string[] = []
for (let idx = 0; idx < document.tokens.length; idx++) {
let token = document.tokens[idx]
if (!token.tokens) {
if ("tokens" in token && token.tokens) {
doc.push(`<div>${standardInnerElem(token)}</div>`)
} else {
doc.push(
`<span class="word" data-idx="${idx}">${token.attrs.head}${token.attrs.word}${token.attrs.tail}</span>`
)
} else {
doc.push(`<div>${standardInnerElem(token.tokens)}</div>`)
}
}
return `${doc.join("")}`
}

function standardOuterElem(data) {
function standardOuterElem(data: TextReaderDataContainer) {
return `<div class="text-container m-md-5">${standardInnerElem(data.document)}</div>`
}

ctrl.$onInit = () => {
$element[0].innerHTML = standardOuterElem(ctrl.data)

$element[0].addEventListener("click", (e) => {
if (e.target.dataset.idx) {
$element[0].addEventListener("click", (e: MouseEvent) => {
const element = e.target as HTMLElement
if (element.dataset.idx) {
document.querySelector(".word.selected")?.classList.remove("selected")
e.target.classList.add("selected")
const idx = e.target.dataset.idx
element.classList.add("selected")
const idx = element.dataset.idx
const token = ctrl.data.document.tokens[idx]
ctrl.wordClick(["wordClick"])(token)
ctrl.wordClick(token)
}
})
}
Expand Down
18 changes: 14 additions & 4 deletions app/scripts/controllers/text_reader_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,33 @@ import { getDataForReadingMode } from "@/backend/backend"
type TextReaderControllerScope = TabHashScope & {
loading: boolean
inData: TextTab
data: { corpus: string; document: TextReaderData; sentenceData: TextTab["sentenceData"] }
data: TextReaderDataContainer
corpusObj: CorpusTransformed
closeTab: (idx: number, e: Event) => void
onentry: () => void
onexit: () => void
}

type TextReaderData = Omit<ApiKwic, "tokens"> & {
export type TextReaderDataContainer = {
corpus: string
document: TextReaderData
sentenceData: TextTab["sentenceData"]
}

export type TextReaderWordHandler = (token: TextReaderToken) => void

export type TextReaderTokenContainer = {
tokens: TextReaderToken[]
}

type TextReaderData = Omit<ApiKwic, "tokens"> & TextReaderTokenContainer

type TextReaderScope = TextReaderControllerScope & {
selectedToken?: TextReaderToken
wordClick: (token: TextReaderToken) => void
wordClick: TextReaderWordHandler
}

type TextReaderToken = TokenTreeParent | TokenTreeLeaf
export type TextReaderToken = TokenTreeParent | TokenTreeLeaf

// TODO The token types need examination, I'm not quite sure yet what the _prepareData function really does

Expand Down

0 comments on commit d809a1f

Please sign in to comment.