constructor(rows, cols) {
this.t = document.createElement('table');
this.t.setAttribute('id', Table.randName(11));
this.t.setAttribute('contenteditable', true);
this.t.setAttribute('class', 'chotki-table');
this.t.style.setProperty('--before-top', '-4px');
this.t.style.setProperty('--before-left', '4px');
this.t.style.setProperty('--after-left', '-4px');
this.t.style.setProperty('--after-top', '4px');
for (let r=0; r<rows; r+=1) {
this.rows.push(this.t.rows[r]);
for (let c=0; c<cols; c+=1) {
this.t.rows[r].insertCell(c);
this.cells.push(this.t.rows[r].cells[c]);
addEvent(el, evType, cb, capture = false, data = {}) {
el.addEventListener(evType, cb, capture);
const bbox = e.target.getBoundingClientRect();
const data = e.target.tdata;
const elbbox = el.getBoundingClientRect();
const leftTop = bbox.x - elbbox.left;
const topLeft = bbox.y - elbbox.top;
console.log('-> bbox: ' , bbox);
console.log('-> elbbox: ' , elbbox);
console.log('leftTop: ', leftTop);
console.log('topLeft: ', topLeft);
if (el.tagName === 'TABLE') {
console.log('table: ', el.tagName);
el.style.setProperty('--before-left', `${leftTop}px`);
el.style.setProperty('--after-top', `${topLeft}px` );
console.log('-> table : ', e.path);
static randName(len, cap = false) {
'a', 'b', 'c', '_', 'd', 'e',
for (let i = 0; i < len; i += 1) {
const rindex = Math.round(Math.random() * chars.length);
randStr.push(chars[rindex]);
const rs = randStr.join('');
return cap ? rs[0].toUpperCase() + rs.slice(1) : rs;
console.log('****** DOMContentLoaded ******');
const mouseEl = document.querySelector('.mouse-position');
const boxEl = document.querySelector('.box');
const box = document.querySelector('.box');
const btn = document.querySelector('.add-btn');
let bbox = box.getBoundingClientRect();
const t = new Table(4, 6);
box.appendChild(t.getTable());
bbox = box.getBoundingClientRect();
let lastPos = { x: 0, y: 0 };
if (lastPos.y>bbox.top && lastPos.y<bbox.bottom &&
lastPos.x > bbox.left && lastPos.x<bbox.right) {
mouseEl.innerHTML = `x: ${lastPos.x} | y: ${lastPos.y}`;
btn.addEventListener('click', addTable);
document.addEventListener('mousemove', mouseMove);