Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/utils/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import jSuites from 'jsuites';
import dispatch from './dispatch.js';
import { getMask, isFormula, updateCell } from './internal.js';
import { setHistory } from './history.js';
import { getCellNameFromCoords } from './helpers.js';

/**
* Open the editor
Expand Down Expand Up @@ -223,10 +224,112 @@ export const openEditor = function (cell, empty, e) {
} else {
editor = createEditor('input');
}
setTimeout(() => {
const editorEl = cell.querySelector('input, textarea');
if (editorEl) {
const table = obj.element;
// delete old handler if any
if (table._cellRefHandler) {
table.removeEventListener('mousedown', table._cellRefHandler, true);
table.removeEventListener('mousemove', table._cellRefHandlerMouseMove, true);
table.removeEventListener('mouseup', table._cellRefHandlerMouseUp, true);
table._cellRefHandler = null;
table._cellRefHandlerMouseMove = null;
table._cellRefHandlerMouseUp = null;
}
let dragStartRef = null;
let dragStartPos = null;
let initialValue = null;
let initialSelectionStart = null;
let initialSelectionEnd = null;
// Handler mousedown : début du drag
table._cellRefHandler = function (ev) {
const targetCell = ev.target.closest('[data-x][data-y]');
if (targetCell && cell.classList.contains('editor')) {
ev.stopImmediatePropagation();
ev.preventDefault();
dragStartRef = getCellNameFromCoords(targetCell.getAttribute('data-x'), targetCell.getAttribute('data-y'));
dragStartPos = {
x: targetCell.getAttribute('data-x'),
y: targetCell.getAttribute('data-y'),
};
initialValue = editorEl.value;
initialSelectionStart = editorEl.selectionStart;
initialSelectionEnd = editorEl.selectionEnd;
// On insère la ref de départ à la position du curseur
editorEl.value = initialValue.slice(0, initialSelectionStart) + dragStartRef + initialValue.slice(initialSelectionEnd);
editorEl.selectionStart = editorEl.selectionEnd = initialSelectionStart + dragStartRef.length;
editorEl.focus();
}
};
// Handler mousemove : ajoute ':' + ref de la cellule survolée
table._cellRefHandlerMouseMove = function (ev) {
if (dragStartRef && cell.classList.contains('editor') && ev.buttons === 1) {
const targetCell = ev.target.closest('[data-x][data-y]');
if (targetCell) {
const refMove = getCellNameFromCoords(targetCell.getAttribute('data-x'), targetCell.getAttribute('data-y'));
// On insère la plage à la position du curseur initial
let refPlage = dragStartRef;
if (refMove !== dragStartRef) {
refPlage = dragStartRef + ':' + refMove;
}
editorEl.value = initialValue.slice(0, initialSelectionStart) + refPlage + initialValue.slice(initialSelectionEnd);
editorEl.selectionStart = editorEl.selectionEnd = initialSelectionStart + refPlage.length;
editorEl.focus();
}
}
};
// Handler mouseup : end zone selection
table._cellRefHandlerMouseUp = function (ev) {
if (dragStartRef && cell.classList.contains('editor')) {
const targetCell = ev.target.closest('[data-x][data-y]');
let refEnd = dragStartRef;
if (targetCell) {
refEnd = getCellNameFromCoords(targetCell.getAttribute('data-x'), targetCell.getAttribute('data-y'));
}
let refPlage = dragStartRef;
if (refEnd !== dragStartRef) {
refPlage = dragStartRef + ':' + refEnd;
}
editorEl.value = initialValue.slice(0, initialSelectionStart) + refPlage + initialValue.slice(initialSelectionEnd);
editorEl.selectionStart = editorEl.selectionEnd = initialSelectionStart + refPlage.length;
editorEl.focus();
dragStartRef = null;
dragStartPos = null;
initialValue = null;
initialSelectionStart = null;
initialSelectionEnd = null;
}
};
table.addEventListener('mousedown', table._cellRefHandler, true);
table.addEventListener('mousemove', table._cellRefHandlerMouseMove, true);
table.addEventListener('mouseup', table._cellRefHandlerMouseUp, true);
}
}, 0);

dispatch.call(obj, 'oncreateeditor', obj, cell, parseInt(x), parseInt(y), null, obj.options.columns[x]);

editor.focus();
// Listener pour valider et déplacer avec les flèches
editor.addEventListener('keydown', function (e) {
if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(e.key)) {
e.preventDefault();
closeEditor.call(obj, cell, true);
let nextX = parseInt(x);
let nextY = parseInt(y);
if (e.key === 'ArrowUp') nextY--;
if (e.key === 'ArrowDown') nextY++;
if (e.key === 'ArrowLeft') nextX--;
if (e.key === 'ArrowRight') nextX++;
// Vérifier que la cellule existe
if (obj.records[nextY] && obj.records[nextY][nextX]) {
const nextCell = obj.records[nextY][nextX].element;
if (nextCell && !nextCell.classList.contains('readonly')) {
openEditor.call(obj, nextCell, false);
}
}
}
});
editor.value = value;

// Column options
Expand Down Expand Up @@ -335,6 +438,7 @@ export const closeEditor = function (cell, save) {
}
}
}
// Add handler for clicking another cell while editing --- Jean-Claude Lhote ---
}
}

Expand Down