Files
Momento/memento-note/scripts/add-resize.js

61 lines
2.1 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const filePath = path.join(__dirname, '..', 'components', 'notes-tabs-view.tsx');
let src = fs.readFileSync(filePath, 'utf8');
const insertCode = `
// Resizable left panel
const [listPanelWidth, setListPanelWidth] = useState(256)
const isDraggingRef = useRef(false)
const handleResizeStart = (e) => {
e.preventDefault()
isDraggingRef.current = true
const startX = e.clientX
const startW = listPanelWidth
const onMove = (ev) => {
if (!isDraggingRef.current) return
setListPanelWidth(Math.min(420, Math.max(180, startW + (ev.clientX - startX))))
}
const onUp = () => {
isDraggingRef.current = false
window.removeEventListener('mousemove', onMove)
window.removeEventListener('mouseup', onUp)
}
window.addEventListener('mousemove', onMove)
window.addEventListener('mouseup', onUp)
}
`;
// Insert before the return statement
const MARKER = ' return (\r\n <div\r\n className="flex min-h-0 flex-1 gap-0';
const ALT_MARKER = ' return (\n <div\n className="flex min-h-0 flex-1 gap-0';
if (src.includes(MARKER) && !src.includes('listPanelWidth')) {
src = src.replace(MARKER, insertCode + MARKER);
console.log('Inserted using CRLF marker');
} else if (src.includes(ALT_MARKER) && !src.includes('listPanelWidth')) {
src = src.replace(ALT_MARKER, insertCode + ALT_MARKER);
console.log('Inserted using LF marker');
} else if (src.includes('listPanelWidth')) {
console.log('State already exists, skipping insert');
} else {
console.error('Marker not found!');
process.exit(1);
}
// Fix left panel width - CRLF version
const OLD_CRLF = 'className="flex w-80 shrink-0 flex-col border-r border-border/60 bg-background">';
const NEW = 'className="flex shrink-0 flex-col border-r border-border/60 bg-background" style={{ width: listPanelWidth }}>';
if (src.includes(OLD_CRLF)) {
src = src.replace(OLD_CRLF, NEW);
console.log('Replaced panel width');
} else {
console.log('Panel width already updated or marker not found');
}
fs.writeFileSync(filePath, src);
console.log('Done!');