<commit_before>import { merge, Symbolic } from '@chialab/proteins';
import { FontAnalyzer } from './font-analyzer.js';
import { LineHeight } from './line-height.js';
import { TextTagger } from './text-tagger.js';

let ids = 1;

const PRIVATE_SYM = new Symbolic('private');

function isSameLine(pos1, pos2) {
    return (pos1.top + pos1.height * 0.5) <= pos2.bottom &&
        (pos2.top + pos2.height * 0.5) <= pos1.bottom;
}

export class A11yText {
    get defaultOptions() {
        return {
            autoLineHeight: true,
            defaultFontSize: 14,
            tagger: {
                modes: ['sentence', 'speaking'],
                useClasses: true,
            },
            focusMode: false,
            highlightWord: true,
            highlightNextWord: true,
            highlightSentence: false,
            highlightLine: true,
            highlightNextSentence: false,
            blockClass: 'a11y-block',
            activeFocusMode: 'a11y-text--active',
            activeWord: 'a11y-text-active-word',
            nextActiveWord: 'a11y-text-next-active-word',
            nextActiveSentence: 'a11y-text-next-active-sentence',
            activeLine: 'a11y-text-active-line',
            activeSentence: 'a11y-text-active-sentence',
            activeBlock: 'a11y-text-active-block',
        };
    }

    constructor(element, options = {}) {
        this[PRIVATE_SYM] = {};
        this.id = `a11y-text-${ids++}`;
        this.options = merge(this.defaultOptions, options);
        this.element = element;
        this.element.setAttribute('data-a11y-id', this.id);
        this.style = document.createElement('style');
        this.style.type = 'text/css';
        this.element.appendChild(this.style);
        if (this.options.autoLineHeight) {
            this.lineHeight = this.calcLineHeight();
            this.paragraphSpacing = this.lineHeightToParagraphSpacing(this.lineHeight);
        }
        if (this.options.tagger) {
            this.tagger = new TextTagger(
                this.options.tagger
            );
            this.tagger.tag(
                this.element
            );
            this.tokens = Array.prototype.slice.call(this.element.querySelectorAll(
                `.${this.tagger.options.tokenSpeaking}, .${this.tagger.options.punctuationClass}`
            ), 0);
            this.speakingTokens = Array.prototype.slice.call(this.element.querySelectorAll(
                `.${this.tagger.options.tokenSpeaking}`
            ), 0);
            this.sentences = Array.prototype.slice.call(this.element.querySelectorAll(
                `.${this.tagger.options.tokenSentence}`
            ), 0);
            let blocks = Array.prototype.slice.call(this.element.querySelectorAll(
                this.tagger.options.blockSelector
            ), 0);
            if (blocks) {
                let blockClass = this.options.blockClass;
                blocks.forEach((block) => {
                    block.classList.add(blockClass);
                });
            }
            if (this.options.focusMode) {
                this.enableFocusMode();
            }<commit_msg>fix: a11y text should use new chunk method<commit_after>import { merge, Symbolic } from '@chialab/proteins';
import { FontAnalyzer } from './font-analyzer.js';
import { LineHeight } from './line-height.js';
import chunk from './chunk.js';

let ids = 1;

const PRIVATE_SYM = new Symbolic('private');

function isSameLine(pos1, pos2) {
    return (pos1.top + pos1.height * 0.5) <= pos2.bottom &&
        (pos2.top + pos2.height * 0.5) <= pos1.bottom;
}

export class A11yText {
    get defaultOptions() {
        return {
            autoLineHeight: true,
            defaultFontSize: 14,
            tagger: {
                modes: ['sentence', 'speaking'],
                useClasses: true,
            },
            focusMode: false,
            highlightWord: true,
            highlightNextWord: true,
            highlightSentence: false,
            highlightLine: true,
            highlightNextSentence: false,
            blockClass: 'a11y-block',
            activeFocusMode: 'a11y-text--active',
            activeWord: 'a11y-text-active-word',
            nextActiveWord: 'a11y-text-next-active-word',
            nextActiveSentence: 'a11y-text-next-active-sentence',
            activeLine: 'a11y-text-active-line',
            activeSentence: 'a11y-text-active-sentence',
            activeBlock: 'a11y-text-active-block',
        };
    }

    constructor(element, options = {}) {
        this[PRIVATE_SYM] = {};
        this.id = `a11y-text-${ids++}`;
        this.options = merge(this.defaultOptions, options);
        this.element = element;
        this.element.setAttribute('data-a11y-id', this.id);
        this.style = document.createElement('style');
        this.style.type = 'text/css';
        this.element.appendChild(this.style);
        if (this.options.autoLineHeight) {
            this.lineHeight = this.calcLineHeight();
            this.paragraphSpacing = this.lineHeightToParagraphSpacing(this.lineHeight);
        }
        if (this.options.tagger) {
            chunk(this.element, this.options.tagger);
            this.tokens = Array.prototype.slice.call(this.element.querySelectorAll(
                `.${this.tagger.options.tokenSpeaking}, .${this.tagger.options.punctuationClass}`
            ), 0);
            this.speakingTokens = Array.prototype.slice.call(this.element.querySelectorAll(
                `.${this.tagger.options.tokenSpeaking}`
            ), 0);
            this.sentences = Array.prototype.slice.call(this.element.querySelectorAll(
                `.${this.tagger.options.tokenSentence}`
            ), 0);
            let blocks = Array.prototype.slice.call(this.element.querySelectorAll(
                this.tagger.options.blockSelector
            ), 0);
            if (blocks) {
                let blockClass = this.options.blockClass;
                blocks.forEach((block) => {
                    block.classList.add(blockClass);
                });
            }
            if (this.options.focusMode) {
                this.enableFocusMode();
            }
{'fraction_non_alphanumeric': 0.12682452156989946, 'fraction_numerical': 0.013623094388582549, 'mean_word_length': 1.8885245901639345, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5239695', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1739, 'n_words': 281}