- fixed displaying dynamic tags;
This commit is contained in:
@@ -4,6 +4,24 @@ import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core';
|
|||||||
import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US';
|
import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US';
|
||||||
import '@univerjs/preset-sheets-core/lib/index.css';
|
import '@univerjs/preset-sheets-core/lib/index.css';
|
||||||
|
|
||||||
|
const TAG_RE = /^\{\{gepafin_field:[^|]+\|[^}]+}}$/;
|
||||||
|
|
||||||
|
const buildTagMap = (workbookData) => {
|
||||||
|
const map = {};
|
||||||
|
if (!workbookData?.sheets) return map;
|
||||||
|
const sheetId = workbookData.sheetOrder?.[0];
|
||||||
|
const sheet = sheetId ? workbookData.sheets[sheetId] : Object.values(workbookData.sheets)[0];
|
||||||
|
if (!sheet?.cellData) return map;
|
||||||
|
Object.entries(sheet.cellData).forEach(([r, row]) => {
|
||||||
|
Object.entries(row).forEach(([c, cell]) => {
|
||||||
|
if (typeof cell.f === 'string' && TAG_RE.test(cell.f)) {
|
||||||
|
map[`${r}:${c}`] = { v: cell.v, f: cell.f };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return map;
|
||||||
|
};
|
||||||
|
|
||||||
const parseWorkbook = (val) => {
|
const parseWorkbook = (val) => {
|
||||||
if (!val) return null;
|
if (!val) return null;
|
||||||
if (typeof val === 'object' && val.sheets) return val;
|
if (typeof val === 'object' && val.sheets) return val;
|
||||||
@@ -23,6 +41,8 @@ const Spreadsheet = ({ fieldName, defaultValue, setDataFn, template }) => {
|
|||||||
const univerRef = useRef(null);
|
const univerRef = useRef(null);
|
||||||
const univerAPIRef = useRef(null);
|
const univerAPIRef = useRef(null);
|
||||||
const saveTimerRef = useRef(null);
|
const saveTimerRef = useRef(null);
|
||||||
|
const isRestoringRef = useRef(false);
|
||||||
|
const tagCellMapRef = useRef({});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!containerRef.current) return;
|
if (!containerRef.current) return;
|
||||||
@@ -43,8 +63,40 @@ const Spreadsheet = ({ fieldName, defaultValue, setDataFn, template }) => {
|
|||||||
univerAPIRef.current = univerAPI;
|
univerAPIRef.current = univerAPI;
|
||||||
|
|
||||||
const initialData = parseWorkbook(defaultValue) || parseWorkbook(template) || { name: 'Sheet' };
|
const initialData = parseWorkbook(defaultValue) || parseWorkbook(template) || { name: 'Sheet' };
|
||||||
|
tagCellMapRef.current = buildTagMap(initialData);
|
||||||
univerAPI.createWorkbook(initialData);
|
univerAPI.createWorkbook(initialData);
|
||||||
|
|
||||||
|
const restoreTagCells = () => {
|
||||||
|
if (isRestoringRef.current) return;
|
||||||
|
const wb = univerAPIRef.current?.getActiveWorkbook();
|
||||||
|
if (!wb) return;
|
||||||
|
const sheet = wb.getActiveSheet();
|
||||||
|
if (!sheet) return;
|
||||||
|
const saved = wb.save();
|
||||||
|
if (!saved?.sheets) return;
|
||||||
|
const sheetId = saved.sheetOrder?.[0];
|
||||||
|
const sheetData = sheetId ? saved.sheets[sheetId] : Object.values(saved.sheets)[0];
|
||||||
|
if (!sheetData?.cellData) return;
|
||||||
|
|
||||||
|
const restorations = [];
|
||||||
|
Object.entries(sheetData.cellData).forEach(([r, row]) => {
|
||||||
|
Object.entries(row).forEach(([c, cell]) => {
|
||||||
|
if (typeof cell.v === 'string' && TAG_RE.test(cell.v)) {
|
||||||
|
const orig = tagCellMapRef.current[`${r}:${c}`];
|
||||||
|
if (orig) restorations.push({ r: +r, c: +c, v: orig.v, f: orig.f });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
if (restorations.length === 0) return;
|
||||||
|
isRestoringRef.current = true;
|
||||||
|
restorations.forEach(({ r, c, v, f }) => {
|
||||||
|
sheet.getRange(r, c, 1, 1).setValues([[{ v, f }]]);
|
||||||
|
});
|
||||||
|
isRestoringRef.current = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
let restoreTimer;
|
||||||
univerAPI.addEvent(univerAPI.Event.BeforeCommandExecute, () => {
|
univerAPI.addEvent(univerAPI.Event.BeforeCommandExecute, () => {
|
||||||
clearTimeout(saveTimerRef.current);
|
clearTimeout(saveTimerRef.current);
|
||||||
saveTimerRef.current = setTimeout(() => {
|
saveTimerRef.current = setTimeout(() => {
|
||||||
@@ -53,10 +105,13 @@ const Spreadsheet = ({ fieldName, defaultValue, setDataFn, template }) => {
|
|||||||
setDataFn(fieldName, wb.save());
|
setDataFn(fieldName, wb.save());
|
||||||
}
|
}
|
||||||
}, 300);
|
}, 300);
|
||||||
|
clearTimeout(restoreTimer);
|
||||||
|
restoreTimer = setTimeout(restoreTagCells, 80);
|
||||||
});
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
clearTimeout(saveTimerRef.current);
|
clearTimeout(saveTimerRef.current);
|
||||||
|
clearTimeout(restoreTimer);
|
||||||
if (univerRef.current) {
|
if (univerRef.current) {
|
||||||
univerRef.current.dispose();
|
univerRef.current.dispose();
|
||||||
univerRef.current = null;
|
univerRef.current = null;
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import { useStoreValue } from '../../../../../../store';
|
|||||||
// tools
|
// tools
|
||||||
import { xlsxToWorkbookData } from '../../../../../BandoEdit/components/BandoEditFormStep3Excel/xlsxToWorkbookData';
|
import { xlsxToWorkbookData } from '../../../../../BandoEdit/components/BandoEditFormStep3Excel/xlsxToWorkbookData';
|
||||||
|
|
||||||
|
const TAG_RE = /^\{\{gepafin_field:([^|]+)\|([^}]+)}}$/;
|
||||||
|
|
||||||
const ElementSettingSpreadsheet = ({ value, name, setDataFn }) => {
|
const ElementSettingSpreadsheet = ({ value, name, setDataFn }) => {
|
||||||
const callFormFields = useStoreValue('callFormFields');
|
const callFormFields = useStoreValue('callFormFields');
|
||||||
const [tagTooltip, setTagTooltip] = useState(null);
|
const [tagTooltip, setTagTooltip] = useState(null);
|
||||||
@@ -19,6 +21,7 @@ const ElementSettingSpreadsheet = ({ value, name, setDataFn }) => {
|
|||||||
const univerRef = useRef(null);
|
const univerRef = useRef(null);
|
||||||
const univerAPIRef = useRef(null);
|
const univerAPIRef = useRef(null);
|
||||||
const formFieldsRef = useRef([]);
|
const formFieldsRef = useRef([]);
|
||||||
|
const isRestoringRef = useRef(false);
|
||||||
const mousePos = useRef({ x: 0, y: 0 });
|
const mousePos = useRef({ x: 0, y: 0 });
|
||||||
const fileInputRef = useRef(null);
|
const fileInputRef = useRef(null);
|
||||||
|
|
||||||
@@ -60,11 +63,54 @@ const ElementSettingSpreadsheet = ({ value, name, setDataFn }) => {
|
|||||||
univerRef.current = univer;
|
univerRef.current = univer;
|
||||||
univerAPIRef.current = univerAPI;
|
univerAPIRef.current = univerAPI;
|
||||||
|
|
||||||
// Disable adding new sheets
|
// Restore tag cells whose display value was overwritten by raw tag on blur
|
||||||
|
const restoreTagCells = () => {
|
||||||
|
if (isRestoringRef.current) return;
|
||||||
|
const wb = univerAPIRef.current?.getActiveWorkbook();
|
||||||
|
if (!wb) return;
|
||||||
|
const sheet = wb.getActiveSheet();
|
||||||
|
if (!sheet) return;
|
||||||
|
const saved = wb.save();
|
||||||
|
if (!saved?.sheets) return;
|
||||||
|
const sheetId = saved.sheetOrder?.[0];
|
||||||
|
const sheetData = sheetId ? saved.sheets[sheetId] : Object.values(saved.sheets)[0];
|
||||||
|
if (!sheetData?.cellData) return;
|
||||||
|
|
||||||
|
const restorations = [];
|
||||||
|
Object.entries(sheetData.cellData).forEach(([r, row]) => {
|
||||||
|
Object.entries(row).forEach(([c, cell]) => {
|
||||||
|
const match = typeof cell.v === 'string' && cell.v.match(TAG_RE);
|
||||||
|
if (match) {
|
||||||
|
const tagIds = match[1].split(',');
|
||||||
|
const type = match[2];
|
||||||
|
const field = formFieldsRef.current.find(f =>
|
||||||
|
f.ids.some(id => tagIds.includes(String(id)))
|
||||||
|
);
|
||||||
|
const displayValue = type === 'value'
|
||||||
|
? (field?.placeholder || '')
|
||||||
|
: (field?.label || match[1]);
|
||||||
|
restorations.push({ r: +r, c: +c, v: displayValue, f: cell.v });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
if (restorations.length === 0) return;
|
||||||
|
isRestoringRef.current = true;
|
||||||
|
restorations.forEach(({ r, c, v, f }) => {
|
||||||
|
sheet.getRange(r, c, 1, 1).setValues([[{ v, f }]]);
|
||||||
|
});
|
||||||
|
isRestoringRef.current = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
let restoreTimer;
|
||||||
|
|
||||||
|
// Disable adding new sheets; also schedule tag-cell restoration after each command
|
||||||
univerAPI.addEvent(univerAPI.Event.BeforeCommandExecute, (event) => {
|
univerAPI.addEvent(univerAPI.Event.BeforeCommandExecute, (event) => {
|
||||||
if (event.id === 'sheet.command.insert-sheet') {
|
if (event.id === 'sheet.command.insert-sheet') {
|
||||||
event.cancel = true;
|
event.cancel = true;
|
||||||
}
|
}
|
||||||
|
clearTimeout(restoreTimer);
|
||||||
|
restoreTimer = setTimeout(restoreTagCells, 80);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Context menu: "Inserisci variabile GEPAFIN"
|
// Context menu: "Inserisci variabile GEPAFIN"
|
||||||
@@ -113,6 +159,7 @@ const ElementSettingSpreadsheet = ({ value, name, setDataFn }) => {
|
|||||||
setTagTooltip(null);
|
setTagTooltip(null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}, []); // refs are stable
|
}, []); // refs are stable
|
||||||
|
|
||||||
// Keep formFieldsRef in sync with store value
|
// Keep formFieldsRef in sync with store value
|
||||||
|
|||||||
Reference in New Issue
Block a user