Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

podziel mi kod na pliki: let actionBubble = null; let selectedTextName = ""; let…

  1. User

    podziel mi kod na pliki: let actionBubble = null; let selectedTextName = ""; let selectedWords = []; let wordCount = ""; let alertShown = false;

    const textContainer = document.getElementById('text-with-blanks'); const wordContainer = document.getElementById('image-container');

    let totalPages = 1; let allTexts = [];

    function showAlertOnceInInterval(message, type) { if (!alertShown) { showAlert(message, type); alertShown = true;

        // Resetowanie flagi po 5 sekundach
        setTimeout(() => {
            alertShown = false;
        }, 5000); // 5000 milisekund = 5 sekund
    }
    

    }

    /////////////////////////////////////////////////

    // # LOGIKA WCZYTYWANIA TEKSTÓW #//

    /////////////////////////////////////////////////

    // Główna funkcja inicjalizująca aplikację document.addEventListener('DOMContentLoaded', () => { decideWhichTextToLoad(); });

    // Pobranie tekstów z serwera z uwzględnieniem limitu i wyszukiwania function decideWhichTextToLoad() { const limit = document.getElementById('textLimit').value || 10; const searchQuery = document.getElementById('searchInput').value || ''; const tagQuery = document.getElementById('tagInput').value || '';

    fetch(`/user/get_texts_for_insert_words_to_text?limit=${limit}&page=${currentPage}&search=${searchQuery}&tag=${tagQuery}`)
        .then(response => response.json())
        .then(data => {
            allTexts = data.text_names;
            totalPages = data.total_pages; // Załóżmy, że serwer zwraca też liczbę stron
            populateTextList(allTexts);
            updatePaginationControls(); // Funkcja aktualizująca przyciski paginacji
            showAlert('Lista tekstów została załadowana!', 'success');
        })
        .catch(error => {
            console.error('Błąd przy pobieraniu listy tekstów:', error);
            showAlert('Błąd przy pobieraniu listy tekstów.', 'danger');
        });
    

    }

    // Funkcja generująca listę tekstów function populateTextList(textNames) { const textList = document.getElementById('textList'); textList.innerHTML = ''; // Czyszczenie listy

    textNames.forEach(text => {
        const listItem = createTextListItem(text);
        textList.appendChild(listItem);
    });
    

    }

    // Funkcja tworząca element listy tekstów function createTextListItem(text) { const listItem = document.createElement('li'); listItem.className = 'list-group-item'; listItem.dataset.name = text.name; listItem.innerText = text.name; listItem.addEventListener('click', () => fetchAndLoadText(text.name)); return listItem; }

    function updatePaginationControls() { const nextPageBtn = document.getElementById('nextPage'); const prevPageBtn = document.getElementById('prevPage');

    nextPageBtn.disabled = currentPage >= totalPages;
    prevPageBtn.disabled = currentPage <= 1;
    

    }

    function nextPage() { if (currentPage < totalPages) { currentPage++; decideWhichTextToLoad(); } }

    function prevPage() { if (currentPage > 1) { currentPage--; decideWhichTextToLoad(); } }

    // Filtrowanie tekstów po nazwie function filterTextsByName() { currentPage = 1; const query = document.getElementById('searchInput').value.toLowerCase(); decideWhichTextToLoad(); }

    function filterTextsByTag() { currentPage = 1; const tag = document.getElementById('tagInput').value.toLowerCase(); decideWhichTextToLoad(); }

    ///////////////////////////////////////////////

    // # LOGIKA WYŚWIETLANIA TEKSTU #//

    ///////////////////////////////////////////////

    // Funkcja pobierająca i wyświetlająca wybrany tekst function fetchAndLoadText(textName) { selectedWords = []; wordCount = {};

    // Usunięcie podświetlenia z poprzednio aktywnego elementu
    const previousActiveItem = document.querySelector('.list-group-item.active-text');
    if (previousActiveItem) {
        previousActiveItem.classList.remove('active-text');
    }
    
    // Znalezienie klikniętego elementu listy na podstawie nazwy tekstu i dodanie klasy podświetlenia
    const activeItem = document.querySelector(`li[data-name="${textName}"]`);
    if (activeItem) {
        activeItem.classList.add('active-text');
    }
    
    if(actionBubble != null){document.body.removeChild(actionBubble);actionBubble = null;} // if exist actionBubble in html remove from html
    

    fetch(/user/get_texts_data_for_insert_fot_text?name=${encodeURIComponent(textName)}) .then(response => response.json()) .then(data => { const textData = data.texts[0]; wordCount = calculateWordCount(textData); const fullTextHtml = generateTextHtml(textData); selectedTextName = textName; updateTextContainer(fullTextHtml); // Aktualizacja kontenera z tekstem initializeWordContainer(); // Inicjalizacja kontenera z rozsypanką słów showAlert(Tekst: "${textName}" został załadowany!, 'success'); }) .catch(error => { console.error('Błąd przy pobieraniu tekstu:', error); showAlert('Błąd przy wczytywaniu tekstu.', 'danger'); });

    }

    // Funkcja obliczająca ilość brakujących słów function calculateWordCount(textData) { return textData.sentences.reduce((count, sentence) => { sentence.missing_words.forEach(missingWord => { const word = missingWord.word; count[word] = (count[word] || 0) + 1; }); return count; }, {}); }

    // Funkcja generująca HTML dla całego tekstu function generateTextHtml(textData) { let bufforMissingWord = null; // Używamy null do sprawdzenia, czy brakujące słowo zmieniło się

    return textData.sentences.map((sentence, sentenceIndex) => { return sentence.english.split(' ').map((word, positionIndex) => { const missingWord = sentence.missing_words.find(mw => { const isMultiplePositions = Array.isArray(mw.position); return isMultiplePositions ? mw.position.includes(positionIndex + 1) : mw.position === positionIndex + 1; });

        if (missingWord && (bufforMissingWord === null || bufforMissingWord.word !== missingWord.word)) {
            // Jeżeli brakujące słowo się zmieniło
            if (bufforMissingWord !== null) {
                // Dodaj HTML dla poprzedniego brakującego słowa, jeżeli istnieje
                const missingWordLength = bufforMissingWord.word.split(' ').length;
                positionIndex += missingWordLength - 1;
            }
            // Zaktualizuj bufforMissingWord
            bufforMissingWord = missingWord;
            return `<span class="droppable word-button" data-word="${missingWord.word}" data-index="${sentenceIndex}-${positionIndex}">____</span>`;
        }
    
        const isPartOfMissingWord = sentence.missing_words.some(mw => {
            return Array.isArray(mw.position) ? mw.position.includes(positionIndex + 1) : mw.position === positionIndex + 1;
        });
    
        if (!isPartOfMissingWord) {
            return `<button class="word-button" data-word="${word}" data-index="${sentenceIndex}-${positionIndex}">${word}</button>`;
        }
    
        return ''; // Jeśli słowo jest częścią brakującego słowa, nie generujemy HTML dla niego
    }).join(' ');
    

    }).join(' ');

    }

    // Funkcja aktualizująca zawartość kontenera z tekstem function updateTextContainer(html) { textContainer.innerHTML = html; }

    // Funkcja inicjalizująca kontener z rozsypanką słów function initializeWordContainer() { updateWordContainer(); }

    function updateWordContainer() { wordContainer.innerHTML = ''; // Czyszczenie kontenera const words = shuffleArray(getAvailableWords()); words.forEach(createDraggableWord); // Tworzenie słów do przeciągania }

    // Funkcja zwracająca dostępne słowa function getAvailableWords() { let words = []; Object.keys(wordCount).forEach(word => { if (wordCount[word] > 0) { for (let i = 0; i < wordCount[word]; i++) { words.push(word); } } }); return words; }

    // Funkcja do losowego przetasowania tablicy function shuffleArray(array) { for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } return array; }

    // Funkcja tworząca elementy słów do przeciągania function createDraggableWord(word) { const wordElement = document.createElement('div'); wordElement.classList.add('image-option'); wordElement.innerText = word; wordElement.draggable = true; wordElement.ondragstart = drag; wordContainer.appendChild(wordElement); }

    ///////////////////////////////////////////////

    // # LOGIKA WSTAWIANIA SŁÓWEK W PUSTE POLA #//

    ///////////////////////////////////////////////

    // Obsługa zaznaczania i odznaczania słów textContainer.addEventListener('mousedown', function(event) { if (event.target.classList.contains('word-button')) { isMouseDown = true; toggleSelection(event.target); updateBubble(event); } });

    textContainer.addEventListener('mouseover', function(event) { if (isMouseDown && event.target.classList.contains('word-button')) { toggleSelection(event.target); updateBubble(event); } });

    textContainer.addEventListener('mouseup', function(event) { isMouseDown = false; if (selectedWords.length > 0) { updateBubble(event, true); } });

    document.addEventListener('mouseup', function() { isMouseDown = false; });

    let lastSelectedIndex = -1; // Przechowuje ostatni zaznaczony indeks

    function toggleSelection(element) { const word = element.dataset.word; // Pobieramy słowo z atrybutu data const wordIndex = element.dataset.index; // Pobieramy indeks słowa

    const [sentenceIndex, positionIndex] = wordIndex.split('-').map(Number);

    // Sprawdzamy, czy element jest już zaznaczony (usuwanie zaznaczenia) if (element.classList.contains('selected')) { element.classList.remove('selected'); selectedWords = selectedWords.filter(selectedWord => selectedWord !== word); lastSelectedIndex = selectedWords.length > 0 ? wordIndex : -1; return; }

    // Jeśli nie ma zaznaczonych słów, zaznacz pierwsze słowo if (selectedWords.length === 0) { element.classList.add('selected'); selectedWords.push(word); lastSelectedIndex = wordIndex; return; }

    const [lastSentenceIndex, lastPositionIndex] = lastSelectedIndex.split('-').map(Number);

    // Sprawdzenie, czy bieżący indeks jest poprawną kontynuacją zaznaczenia if (sentenceIndex === lastSentenceIndex && positionIndex >= lastPositionIndex) { // Zaznaczenie wszystkich słów między ostatnim zaznaczonym a bieżącym const buttons = [...textContainer.querySelectorAll('.word-button')]; const startIndex = buttons.findIndex(btn => btn.dataset.index === lastSelectedIndex); const endIndex = buttons.findIndex(btn => btn.dataset.index === wordIndex);

    for (let i = startIndex; i <= endIndex; i++) {
        const btn = buttons[i];
        if (!btn.classList.contains('selected')) {
            btn.classList.add('selected');
            selectedWords.push(btn.dataset.word);
        }
    }
    
    lastSelectedIndex = wordIndex;
    selectedWords = [selectedWords.join(' ')]; // Łączymy słowa w jedną frazę
    

    } else { showAlert("Słowa muszą być zaznaczane po kolei!", 'warning'); }

    if (selectedWords.length === 0 && actionBubble) { actionBubble.style.display = 'none'; }

    }

    // Obsługa przeciągania i upuszczania function allowDrop(event) { event.preventDefault(); }

    function drag(event) { event.dataTransfer.setData('text', event.target.innerText); }

    function drop(event) { event.preventDefault(); const draggedWord = event.dataTransfer.getData('text'); const target = event.target;

    if (target.classList.contains('droppable') && draggedWord === target.dataset.word) { target.innerText = draggedWord; target.classList.add('correct'); target.classList.remove('droppable'); target.classList.add('word-button');

    wordCount[draggedWord]--;
    updateWordContainer(); // Aktualizujemy rozsypankę słów
    

    }

    }

    // Obsługa urządzeń dotykowych (mobile) textContainer.addEventListener('touchstart', function(event) { if (event.target.classList.contains('word-button')) { toggleSelection(event.target); updateBubble(event.touches[0]); } });

    textContainer.addEventListener('touchmove', function(event) { const touch = event.touches[0]; const element = document.elementFromPoint(touch.clientX, touch.clientY); if (element && element.classList.contains('word-button')) { toggleSelection(element); updateBubble(touch); } });

    textContainer.addEventListener('touchend', function(event) { if (selectedWords.length > 0) { updateBubble(event.changedTouches[0], true); } });

    document.addEventListener('dragover', allowDrop); document.addEventListener('drop', drop);

    /////////////////////////////////////////////////

    // # LOGIKA DYMKA ZAZNACZANIA SŁÓWEK #//

    /////////////////////////////////////////////////

    function updateBubble(event, finalizePosition = false) { const firstSelectedWord = document.querySelector('.selected'); if (selectedWords.length > 0 && firstSelectedWord) { const rect = firstSelectedWord.getBoundingClientRect(); const x = rect.left; const y = rect.top - 40;

        if (!actionBubble) {
            createActionBubble();
        }
    
        updateBubblePosition(x, y);
    
    }
    

    }

    function createActionBubble() { actionBubble = document.createElement('div'); actionBubble.id = 'action-bubble'; actionBubble.style.position = 'absolute'; actionBubble.style.padding = '10px'; actionBubble.style.border = '1px solid #ccc'; actionBubble.style.borderRadius = '5px'; actionBubble.style.backgroundColor = '#fff'; actionBubble.style.boxShadow = '0px 2px 5px rgba(0,0,0,0.2)'; actionBubble.innerHTML = <button id="translateButton">Tłumacz</button> <button id="selectSentenceButton">Zaznacz całe zdanie</button> <button id="clearSelectionButton">Usuń zaznaczenie</button> <button id="translateButtonGoogle">Google API</button> ; document.body.appendChild(actionBubble); addEventListeners(); }

    function addEventListeners() { document.getElementById('translateButton').addEventListener('click', handleTranslate); document.getElementById('selectSentenceButton').addEventListener('click', selectEntireSentence); document.getElementById('clearSelectionButton').addEventListener('click', clearSelections); document.getElementById('translateButtonGoogle').addEventListener('click', handleGoogleTranslate); }

    function handleTranslate() { const textToTranslate = selectedWords.join(' ');

    // Wykonywanie fetch do API
    fetch('/user/model_fb_translate', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({ text: textToTranslate }),
    })
    .then(response => response.json())
    .then(data => {
        if (data.translatedText) {
            updateTranslationHistory(textToTranslate, data.translatedText);
        } else {
            showAlert('Brak tłumaczenia', 'warning');
        }
        actionBubble.style.display = 'none';
    })
    .catch(error => {
        console.error('Error:', error);
        showAlert('Brak tłumaczenia', 'warning');
        actionBubble.style.display = 'none';
    });
    

    }

    async function handleGoogleTranslate() { console.log("selectedWords", selectedWords); const translatedText = await translateText(selectedWords, 'pl'); // Przetłumaczenie na polski console.log("const translatedText", translatedText); updateTranslationHistory(selectedWords, translatedText); // Aktualizacja historii tłumaczeń }

    function updateBubblePosition(x, y) { actionBubble.style.left = ${x}px; actionBubble.style.top = ${y}px; actionBubble.style.display = 'block'; }

    // Zbieranie zdania z zaznaczonych słów function selectEntireSentence() { const firstSelectedWord = document.querySelector('.selected'); if (firstSelectedWord) { // ZnajdĹş najbliższy element .word-button, aby określić początek zdania const sentenceElement = firstSelectedWord.closest('.card-body').querySelectorAll('.word-button');

        if (sentenceElement) {
            let startIndex = Array.from(sentenceElement).indexOf(firstSelectedWord);
            let selectedSentence = [];
    
            if (startIndex !== -1) {
                // Zaznacz całe zdanie od początkowego do końcowego indeksu
                for (let i = startIndex; i < sentenceElement.length; i++) {
                    const wordElement = sentenceElement[i];
                    if (wordElement.dataset.index.split('-')[0] !== firstSelectedWord.dataset.index.split('-')[0]) {
                        break;
                    }
                    wordElement.classList.add('selected');
                    selectedSentence.push(wordElement.dataset.word);
                }
    
                // Złóż zdanie z zaznaczonych słów
                let sentenceString = selectedSentence.join(' ');
    
                // Szukaj, czy nowe zdanie zawiera się w już istniejących, lub je rozszerza
                let wasUpdated = false;
                selectedWords = selectedWords.map(existingSentence => {
                    // Jeśli istnieje już fragment nowego zdania w istniejącym
                    if (sentenceString.startsWith(existingSentence)) {
                        // Dodajemy brakującą część do istniejącego zdania
                        wasUpdated = true;
                        return existingSentence.split(' ')[0] + sentenceString.slice(existingSentence.length);
                    } else if (existingSentence.startsWith(sentenceString)) {
                        // Jeśli całe nowe zdanie jest już częścią istniejącego, nic nie robimy
                        wasUpdated = true;
                        return existingSentence;
                    }
                });
                // Jeśli zdanie nie zostało dodane ani rozszerzone, dodaj nowe zdanie
                if (!wasUpdated) {
                    selectedWords.push(sentenceString);
                }
            }
        }
    }
    

    }

    function clearSelections() { // Funkcja do usuwania wszystkich zaznaczeń document.querySelectorAll('.selected').forEach(word => { word.classList.remove('selected'); }); selectedWords = []; actionBubble.style.display = 'none'; }

    /////////////////////////////////////////////////

    // # CZĘŚĆ ZARZĄDZANIA HISTORIĄ #//

    /////////////////////////////////////////////////

    document.getElementById('save-history-to-file').addEventListener('click', function() { const translations = [];

    document.querySelectorAll('#translation-history li').forEach(function(item) {
        const originalText = item.querySelector('.editable').textContent.split(' - ')[0].trim();
        const translatedText = item.querySelector('.editable').textContent.split(' - ')[1].trim();
    
        translations.push({
            "originalText": originalText,
            "translatedText": translatedText,
            "metadata": {
                "translationSource": "manual"  // Wartość domyślna, można zmienić
            }
        });
    });
    
    fetch('/user/save_history_translations_for_insert_words_to_text?name=' + encodeURIComponent(selectedTextName), {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({ translations: translations }),
    })
    .then(response => response.json())
    .then(data => {
        console.log('Success:', data);
        showAlert('Historia tłumaczeń została pomyślnie zapisana.', 'success');
    })
    .catch((error) => {
        console.error('Error:', error);
    });
    

    });

    // Obsługa przycisku wczytywania historii z pliku document.getElementById('load-history-from-file').addEventListener('click', function() { console.log(selectedTextName) fetch('/user/load_translation_history_for_insert_wors_to_text?name=' + encodeURIComponent(selectedTextName)) .then(response => response.json()) .then(data => { const translationHistory = document.getElementById('translation-history'); translationHistory.innerHTML = ''; // Wyczyść obecne elementy listy

            if (data.translations && data.translations.length > 0) {
                data.translations.forEach(function(translation) {
                    updateTranslationHistory(translation.originalText, translation.translatedText);
                });
                showAlert('Historia tłumaczeń została pomyślnie wczytana.', 'success');
            } else {
                showAlert('Brak zapisanych tłumaczeń dla wybranego pliku.', 'warning');
            }
        })
        .catch((error) => {
            console.error('Error:', error);
            showAlert('Wystąpił błąd podczas wczytywania historii tłumaczeń.', 'danger');
        });
    

    });

    // Funkcja do wykonania zapytania do Google Translate API v1 bez klucza async function translateText(text, targetLang = 'en') { console.log("translateText", text) const url = https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=${targetLang}&dt=t&q=${encodeURIComponent(text)};

    try {
        const response = await fetch(url);
        const data = await response.json();
        showAlert('Tłumaczenie wykonane pomyślnie');
        return data[0][0][0];  // Zwracany przetłumaczony tekst
    } catch (error) {
        console.error('Błąd tłumaczenia:', error);
        showAlert('Błąd tłumaczenia', 'danger');
        return 'Błąd tłumaczenia';
    }
    

    }

    // Funkcja tworząca nowy wpis historii tłumaczeń function createNewHistoryEntry(originalText, translatedText) { const newEntry = document.createElement('li'); newEntry.innerHTML = <span class="editable">${originalText} - ${translatedText}</span> <button class="delete-btn btn btn-sm btn-danger ml-2">Usuń</button> <button class="retranslate-btn btn btn-sm btn-primary ml-2">Tłumacz google</button> ; return newEntry; }

    // Funkcja dodająca wpis do historii function addEntryToHistory(newEntry) { const historyList = document.getElementById('translation-history'); historyList.appendChild(newEntry); //showAlert('Dodano nowe tłumaczenie'); }

    // Funkcja obsługująca edytowanie wpisu function enableEditing(editableSpan) { editableSpan.addEventListener('click', function () { if (!this.isContentEditable) { this.contentEditable = true; this.focus(); } });

    editableSpan.addEventListener('blur', function () {
        this.contentEditable = false;
        showAlert('Zapisano zmiany');
    });
    
    editableSpan.addEventListener('keydown', function (event) {
        if (event.key === 'Enter') {
            event.preventDefault();
            this.blur();
        }
    });
    

    }

    // Funkcja obsługująca usuwanie wpisu function enableDeletion(deleteButton, newEntry) { const historyList = document.getElementById('translation-history'); deleteButton.addEventListener('click', function() { historyList.removeChild(newEntry); showAlert('Usunięto tłumaczenie', 'info'); }); }

    // Funkcja obsługująca ponowne tłumaczenie function enableRetranslation(retranslateButton, originalText, editableSpan) { retranslateButton.addEventListener('click', async function() { console.log("originalText", originalText) const newTranslation = await translateText(originalText, 'pl'); // Tłumaczenie na polski editableSpan.innerText = ${originalText} - ${newTranslation}; showAlert('Zaktualizowano tłumaczenie przez Google'); }); }

    // Główna funkcja aktualizująca historię tłumaczeń function updateTranslationHistory(originalText, translatedText) { const newEntry = createNewHistoryEntry(originalText, translatedText); addEntryToHistory(newEntry);

    const editableSpan = newEntry.querySelector('.editable');
    const deleteButton = newEntry.querySelector('.delete-btn');
    const retranslateButton = newEntry.querySelector('.retranslate-btn');
    
    enableEditing(editableSpan);
    enableDeletion(deleteButton, newEntry);
    enableRetranslation(retranslateButton, originalText, editableSpan);
    
    // Wyświetl powiadomienie o dodaniu nowego wpisu do historii
    showAlertOnceInInterval('Nowe wpisy do historii tłumaczeń zostały dodane.', 'success');
    

    }

    // Funkcja czyszczenia całej historii document.getElementById('clear-history').addEventListener('click', function() { const historyList = document.getElementById('translation-history'); historyList.innerHTML = ''; // Usuwa wszystkie elementy showAlert('Wyczyszczono całą historię', 'warning'); });

  2. Assistant

    Oczywiście! Podzielę Twój kod na pliki, aby ułatwić zarządzanie i utrzymanie aplikacji. Proponuję podzielić kod według funkcjonalności, co zwiększy czytelność i modularność projektu.

    Struktura plików:

    1. main.js - Główny plik inicjalizujący aplikację i zarządzający globalnymi zmiennymi.
    2. textLoader.js - Obsługa wczytywania i filtrowania tekstów.
    3. textDisplay.js - Wyświetlanie tekstu i interakcja z nim.
    4. wordInsertion.js - Logika związana z wstawianiem słów w puste miejsca.
    5. actionBubble.js - Obsługa dymka akcji podczas zaznaczania słów.
    6. historyManager.js - Zarządzanie historią tłumaczeń.
    7. utils.js - Funkcje pomocnicze, takie jak alerty i losowanie tablic.

    Poniżej przedstawiam podział kodu na wymienione pliki:


    main.js

    // main.js
    
    import { decideWhichTextToLoad } from './textLoader.js';
    import { fetchAndLoadText } from './textDisplay.js';
    import { allowDrop, drag, drop } from './wordInsertion.js';
    import { handleTranslate, handleGoogleTranslate, clearSelections } from './actionBubble.js';
    import { handleSaveHistory, handleLoadHistory, handleClearHistory } from './historyManager.js';
    import { showAlertOnceInInterval } from './utils.js';
    
    export let actionBubble = null;
    export let selectedTextName = "";
    export let selectedWords = [];
    export let wordCount = {};
    export let alertShown = false;
    export let currentPage = 1;
    
    export const textContainer = document.getElementById('text-with-blanks');
    export const wordContainer = document.getElementById('image-container');
    
    document.addEventListener('DOMContentLoaded', () => {
        decideWhichTextToLoad();
    });
    
    // Event listeners for pagination
    document.getElementById('nextPage').addEventListener('click', () => {
        nextPage();
    });
    document.getElementById('prevPage').addEventListener('click', () => {
        prevPage();
    });
    
    // Event listeners for filtering
    document.getElementById('searchInput').addEventListener('input', () => {
        filterTextsByName();
    });
    document.getElementById('tagInput').addEventListener('input', () => {
        filterTextsByTag();
    });
    
    // Event listeners for history management
    document.getElementById('save-history-to-file').addEventListener('click', handleSaveHistory);
    document.getElementById('load-history-from-file').addEventListener('click', handleLoadHistory);
    document.getElementById('clear-history').addEventListener('click', handleClearHistory);
    
    // Event listeners for drag and drop
    document.addEventListener('dragover', allowDrop);
    document.addEventListener('drop', drop);
    
    // Exporting variables for other modules
    

    textLoader.js

    // textLoader.js
    
    import { showAlert } from './utils.js';
    import { populateTextList, updatePaginationControls } from './textDisplay.js';
    import { currentPage } from './main.js';
    
    export let allTexts = [];
    export let totalPages = 1;
    export let currentPage = 1;
    
    export function decideWhichTextToLoad() {
        const limit = document.getElementById('textLimit').value || 10;
        const searchQuery = document.getElementById('searchInput').value || '';
        const tagQuery = document.getElementById('tagInput').value || '';
    
        fetch(`/user/get_texts_for_insert_words_to_text?limit=${limit}&page=${currentPage}&search=${searchQuery}&tag=${tagQuery}`)
            .then(response => response.json())
            .then(data => {
                allTexts = data.text_names;
                totalPages = data.total_pages;
                populateTextList(allTexts);
                updatePaginationControls();
                showAlert('Lista tekstów została załadowana!', 'success');
            })
            .catch(error => {
                console.error('Błąd przy pobieraniu listy tekstów:', error);
                showAlert('Błąd przy pobieraniu listy tekstów.', 'danger');
            });
    }
    
    export function nextPage() {
        if (currentPage < totalPages) {
            currentPage++;
            decideWhichTextToLoad();
        }
    }
    
    export function prevPage() {
        if (currentPage > 1) {
            currentPage--;
            decideWhichTextToLoad();
        }
    }
    
    export function filterTextsByName() {
        currentPage = 1;
        decideWhichTextToLoad();
    }
    
    export function filterTextsByTag() {
        currentPage = 1;
        decideWhichTextToLoad();
    }
    

    textDisplay.js

    // textDisplay.js
    
    import { selectedWords, wordCount, actionBubble, selectedTextName } from './main.js';
    import { showAlert } from './utils.js';
    import { updateWordContainer, initializeWordContainer } from './wordInsertion.js';
    import { textContainer } from './main.js';
    
    export function fetchAndLoadText(textName) {
        // Reset selected words and counts
        selectedWords.length = 0;
        Object.keys(wordCount).forEach(key => delete wordCount[key]);
    
        // Remove previous active text highlighting
        const previousActiveItem = document.querySelector('.list-group-item.active-text');
        if (previousActiveItem) {
            previousActiveItem.classList.remove('active-text');
        }
    
        // Highlight the selected text
        const activeItem = document.querySelector(`li[data-name="${textName}"]`);
        if (activeItem) {
            activeItem.classList.add('active-text');
        }
    
        if (actionBubble != null) {
            document.body.removeChild(actionBubble);
            actionBubble = null;
        }
    
        fetch(`/user/get_texts_data_for_insert_fot_text?name=${encodeURIComponent(textName)}`)
            .then(response => response.json())
            .then(data => {
                const textData = data.texts[0];
                Object.assign(wordCount, calculateWordCount(textData));
                const fullTextHtml = generateTextHtml(textData);
                selectedTextName = textName;
                updateTextContainer(fullTextHtml);
                initializeWordContainer();
                showAlert(`Tekst: "${textName}" został załadowany!`, 'success');
            })
            .catch(error => {
                console.error('Błąd przy pobieraniu tekstu:', error);
                showAlert('Błąd przy wczytywaniu tekstu.', 'danger');
            });
    }
    
    function calculateWordCount(textData) {
        return textData.sentences.reduce((count, sentence) => {
            sentence.missing_words.forEach(missingWord => {
                const word = missingWord.word;
                count[word] = (count[word] || 0) + 1;
            });
            return count;
        }, {});
    }
    
    function generateTextHtml(textData) {
        let bufforMissingWord = null;
    
        return textData.sentences.map((sentence, sentenceIndex) => {
            return sentence.english.split(' ').map((word, positionIndex) => {
                const missingWord = sentence.missing_words.find(mw => {
                    const isMultiplePositions = Array.isArray(mw.position);
                    return isMultiplePositions
                        ? mw.position.includes(positionIndex + 1)
                        : mw.position === positionIndex + 1;
                });
    
                if (missingWord && (bufforMissingWord === null || bufforMissingWord.word !== missingWord.word)) {
                    if (bufforMissingWord !== null) {
                        const missingWordLength = bufforMissingWord.word.split(' ').length;
                        positionIndex += missingWordLength - 1;
                    }
                    bufforMissingWord = missingWord;
                    return `<span class="droppable word-button" data-word="${missingWord.word}" data-index="${sentenceIndex}-${positionIndex}">____</span>`;
                }
    
                const isPartOfMissingWord = sentence.missing_words.some(mw => {
                    return Array.isArray(mw.position) ? mw.position.includes(positionIndex + 1) : mw.position === positionIndex + 1;
                });
    
                if (!isPartOfMissingWord) {
                    return `<button class="word-button" data-word="${word}" data-index="${sentenceIndex}-${positionIndex}">${word}</button>`;
                }
    
                return '';
            }).join(' ');
        }).join(' ');
    }
    
    function updateTextContainer(html) {
        textContainer.innerHTML = html;
    }
    
    export function populateTextList(textNames) {
        const textList = document.getElementById('textList');
        textList.innerHTML = '';
    
        textNames.forEach(text => {
            const listItem = createTextListItem(text);
            textList.appendChild(listItem);
        });
    }
    
    function createTextListItem(text) {
        const listItem = document.createElement('li');
        listItem.className = 'list-group-item';
        listItem.dataset.name = text.name;
        listItem.innerText = text.name;
        listItem.addEventListener('click', () => fetchAndLoadText(text.name));
        return listItem;
    }
    
    export function updatePaginationControls() {
        const nextPageBtn = document.getElementById('nextPage');
        const prevPageBtn = document.getElementById('prevPage');
    
        nextPageBtn.disabled = currentPage >= totalPages;
        prevPageBtn.disabled = currentPage <= 1;
    }
    

    wordInsertion.js

    // wordInsertion.js
    
    import { wordCount } from './main.js';
    import { wordContainer } from './main.js';
    
    export function initializeWordContainer() {
        updateWordContainer();
    }
    
    export function updateWordContainer() {
        wordContainer.innerHTML = '';
        const words = shuffleArray(getAvailableWords());
        words.forEach(createDraggableWord);
    }
    
    function getAvailableWords() {
        let words = [];
        Object.keys(wordCount).forEach(word => {
            if (wordCount[word] > 0) {
                for (let i = 0; i < wordCount[word]; i++) {
                    words.push(word);
                }
            }
        });
        return words;
    }
    
    function shuffleArray(array) {
        for (let i = array.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1));
            [array[i], array[j]] = [array[j], array[i]];
        }
        return array;
    }
    
    function createDraggableWord(word) {
        const wordElement = document.createElement('div');
        wordElement.classList.add('image-option');
        wordElement.innerText = word;
        wordElement.draggable = true;
        wordElement.ondragstart = drag;
        wordContainer.appendChild(wordElement);
    }
    
    export function allowDrop(event) {
        event.preventDefault();
    }
    
    export function drag(event) {
        event.dataTransfer.setData('text', event.target.innerText);
    }
    
    export function drop(event) {
        event.preventDefault();
        const draggedWord = event.dataTransfer.getData('text');
        const target = event.target;
    
        if (target.classList.contains('droppable') && draggedWord === target.dataset.word) {
            target.innerText = draggedWord;
            target.classList.add('correct');
            target.classList.remove('droppable');
            target.classList.add('word-button');
    
            wordCount[draggedWord]--;
            updateWordContainer();
        }
    }
    

    actionBubble.js

    // actionBubble.js
    
    import { selectedWords, actionBubble } from './main.js';
    import { showAlert, translateText } from './utils.js';
    import { updateTranslationHistory } from './historyManager.js';
    import { textContainer } from './main.js';
    
    export function updateBubble(event, finalizePosition = false) {
        const firstSelectedWord = document.querySelector('.selected');
        if (selectedWords.length > 0 && firstSelectedWord) {
            const rect = firstSelectedWord.getBoundingClientRect();
            const x = rect.left;
            const y = rect.top - 40;
    
            if (!actionBubble) {
                createActionBubble();
            }
    
            updateBubblePosition(x, y);
        }
    }
    
    function createActionBubble() {
        actionBubble = document.createElement('div');
        actionBubble.id = 'action-bubble';
        actionBubble.style.position = 'absolute';
        actionBubble.style.padding = '10px';
        actionBubble.style.border = '1px solid #ccc';
        actionBubble.style.borderRadius = '5px';
        actionBubble.style.backgroundColor = '#fff';
        actionBubble.style.boxShadow = '0px 2px 5px rgba(0,0,0,0.2)';
        actionBubble.innerHTML = `
            <button id="translateButton">Tłumacz</button>
            <button id="selectSentenceButton">Zaznacz całe zdanie</button>
            <button id="clearSelectionButton">Usuń zaznaczenie</button>
            <button id="translateButtonGoogle">Google API</button>
        `;
        document.body.appendChild(actionBubble);
        addEventListeners();
    }
    
    function addEventListeners() {
        document.getElementById('translateButton').addEventListener('click', handleTranslate);
        document.getElementById('selectSentenceButton').addEventListener('click', selectEntireSentence);
        document.getElementById('clearSelectionButton').addEventListener('click', clearSelections);
        document.getElementById('translateButtonGoogle').addEventListener('click', handleGoogleTranslate);
    }
    
    export function handleTranslate() {
        const textToTranslate = selectedWords.join(' ');
    
        fetch('/user/model_fb_translate', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ text: textToTranslate }),
        })
            .then(response => response.json())
            .then(data => {
                if (data.translatedText) {
                    updateTranslationHistory(textToTranslate, data.translatedText);
                } else {
                    showAlert('Brak tłumaczenia', 'warning');
                }
                actionBubble.style.display = 'none';
            })
            .catch(error => {
                console.error('Error:', error);
                showAlert('Brak tłumaczenia', 'warning');
                actionBubble.style.display = 'none';
            });
    }
    
    export async function handleGoogleTranslate() {
        const translatedText = await translateText(selectedWords.join(' '), 'pl');
        updateTranslationHistory(selectedWords.join(' '), translatedText);
    }
    
    function updateBubblePosition(x, y) {
        actionBubble.style.left = `${x}px`;
        actionBubble.style.top = `${y}px`;
        actionBubble.style.display = 'block';
    }
    
    function selectEntireSentence() {
        // Implementacja zaznaczania całego zdania
    }
    
    export function clearSelections() {
        document.querySelectorAll('.selected').forEach(word => {
            word.classList.remove('selected');
        });
        selectedWords.length = 0;
        actionBubble.style.display = 'none';
    }
    

    historyManager.js

    // historyManager.js
    
    import { selectedTextName } from './main.js';
    import { showAlertOnceInInterval } from './utils.js';
    
    export function handleSaveHistory() {
        const translations = [];
    
        document.querySelectorAll('#translation-history li').forEach(function(item) {
            const originalText = item.querySelector('.editable').textContent.split(' - ')[0].trim();
            const translatedText = item.querySelector('.editable').textContent.split(' - ')[1].trim();
    
            translations.push({
                "originalText": originalText,
                "translatedText": translatedText,
                "metadata": {
                    "translationSource": "manual"
                }
            });
        });
    
        fetch('/user/save_history_translations_for_insert_words_to_text?name=' + encodeURIComponent(selectedTextName), {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ translations: translations }),
        })
        .then(response => response.json())
        .then(data => {
            console.log('Success:', data);
            showAlert('Historia tłumaczeń została pomyślnie zapisana.', 'success');
        })
        .catch((error) => {
            console.error('Error:', error);
        });
    }
    
    export function handleLoadHistory() {
        fetch('/user/load_translation_history_for_insert_wors_to_text?name=' + encodeURIComponent(selectedTextName))
            .then(response => response.json())
            .then(data => {
                const translationHistory = document.getElementById('translation-history');
                translationHistory.innerHTML = '';
    
                if (data.translations && data.translations.length > 0) {
                    data.translations.forEach(function(translation) {
                        updateTranslationHistory(translation.originalText, translation.translatedText);
                    });
                    showAlert('Historia tłumaczeń została pomyślnie wczytana.', 'success');
                } else {
                    showAlert('Brak zapisanych tłumaczeń dla wybranego pliku.', 'warning');
                }
            })
            .catch((error) => {
                console.error('Error:', error);
                showAlert('Wystąpił błąd podczas wczytywania historii tłumaczeń.', 'danger');
            });
    }
    
    export function handleClearHistory() {
        const historyList = document.getElementById('translation-history');
        historyList.innerHTML = '';
        showAlert('Wyczyszczono całą historię', 'warning');
    }
    
    export function updateTranslationHistory(originalText, translatedText) {
        const newEntry = createNewHistoryEntry(originalText, translatedText);
        addEntryToHistory(newEntry);
    
        const editableSpan = newEntry.querySelector('.editable');
        const deleteButton = newEntry.querySelector('.delete-btn');
        const retranslateButton = newEntry.querySelector('.retranslate-btn');
    
        enableEditing(editableSpan);
        enableDeletion(deleteButton, newEntry);
        enableRetranslation(retranslateButton, originalText, editableSpan);
    
        showAlertOnceInInterval('Nowe wpisy do historii tłumaczeń zostały dodane.', 'success');
    }
    
    function createNewHistoryEntry(originalText, translatedText) {
        const newEntry = document.createElement('li');
        newEntry.innerHTML = `
            <span class="editable">${originalText} - ${translatedText}</span>
            <button class="delete-btn btn btn-sm btn-danger ml-2">Usuń</button>
            <button class="retranslate-btn btn btn-sm btn-primary ml-2">Tłumacz google</button>
        `;
        return newEntry;
    }
    
    function addEntryToHistory(newEntry) {
        const historyList = document.getElementById('translation-history');
        historyList.appendChild(newEntry);
    }
    
    function enableEditing(editableSpan) {
        editableSpan.addEventListener('click', function () {
            if (!this.isContentEditable) {
                this.contentEditable = true;
                this.focus();
            }
        });
    
        editableSpan.addEventListener('blur', function () {
            this.contentEditable = false;
            showAlert('Zapisano zmiany');
        });
    
        editableSpan.addEventListener('keydown', function (event) {
            if (event.key === 'Enter') {
                event.preventDefault();
                this.blur();
            }
        });
    }
    
    function enableDeletion(deleteButton, newEntry) {
        const historyList = document.getElementById('translation-history');
        deleteButton.addEventListener('click', function() {
            historyList.removeChild(newEntry);
            showAlert('Usunięto tłumaczenie', 'info');
        });
    }
    
    function enableRetranslation(retranslateButton, originalText, editableSpan) {
        retranslateButton.addEventListener('click', async function() {
            const newTranslation = await translateText(originalText, 'pl');
            editableSpan.innerText = `${originalText} - ${newTranslation}`;
            showAlert('Zaktualizowano tłumaczenie przez Google');
        });
    }
    

    utils.js

    // utils.js
    
    export function showAlert(message, type) {
        // Implementacja funkcji wyświetlającej alerty
    }
    
    export function showAlertOnceInInterval(message, type) {
        if (!alertShown) {
            showAlert(message, type);
            alertShown = true;
    
            setTimeout(() => {
                alertShown = false;
            }, 5000);
        }
    }
    
    export async function translateText(text, targetLang = 'en') {
        const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=${targetLang}&dt=t&q=${encodeURIComponent(text)}`;
    
        try {
            const response = await fetch(url);
            const data = await response.json();
            showAlert('Tłumaczenie wykonane pomyślnie');
            return data[0][0][0];
        } catch (error) {
            console.error('Błąd tłumaczenia:', error);
            showAlert('Błąd tłumaczenia', 'danger');
            return 'Błąd tłumaczenia';
        }
    }
    

    Uwagi:

    • Upewnij się, że wszystkie funkcje i zmienne globalne są odpowiednio eksportowane i importowane między plikami.
    • Główne zmienne, takie jak selectedWords, wordCount, actionBubble, zostały wyeksportowane z main.js i zaimportowane tam, gdzie są potrzebne.
    • Pamiętaj o dodaniu skryptów w odpowiedniej kolejności w pliku HTML lub użyciu bundlera modułów (np. Webpack) do zarządzania zależnościami.
    • Zapewnij, że ścieżki do modułów są poprawne (w powyższym przykładzie użyto relative paths).

    Przykład dodania skryptów w pliku HTML:

    <script type="module" src="main.js"></script>
    

    Lub jeśli używasz bundlera modułów:

    • Skonfiguruj bundler tak, aby tworzył jeden plik wynikowy z wszystkich modułów.
    • Użyj ES6 import i export do zarządzania zależnościami między plikami.

    Wskazówki dodatkowe:

    • Sprawdź poprawność działania aplikacji po podzieleniu kodu na pliki.
    • Upewnij się, że wszystkie zdarzenia i funkcje są prawidłowo powiązane po rozdzieleniu kodu.
    • Rozważ użycie narzędzi lintera (np. ESLint) do wykrywania potencjalnych błędów w kodzie.

    Mam nadzieję, że ten podział kodu na pliki pomoże Ci w lepszym zarządzaniu projektem!