import { Editor } from "./editor.js"; import { checkScene } from "./error_check.js"; import { logger } from "./log.js"; import request from "./request.js"; import http from "./http.js"; function reloadWorldList(worldList, done) { var xhr = new XMLHttpRequest(); // we defined the xhr xhr.onreadystatechange = function () { if (this.readyState != 4) return; if (this.status == 200) { let anns = JSON.parse(this.responseText); // load annotations anns.forEach((a) => { let world = worldList.find((w) => { return w.frameInfo.scene == a.scene && w.frameInfo.frame == a.frame; }); if (world) { world.annotation.reapplyAnnotation(a.annotation); } else { console.error("bug?"); } }); if (done) done(); } }; xhr.open("POST", request + "/loadworldlist", true); let para = worldList.map((w) => { return { //todo: we could add an id, so as to associate world easily scene: w.frameInfo.scene, frame: w.frameInfo.frame, }; }); xhr.send(JSON.stringify(para)); } var saveDelayTimer = null; var pendingSaveList = []; function saveWorldList(worldList) { //pendingSaveList = pendingSaveList.concat(worldList); worldList.forEach((w) => { if (!pendingSaveList.includes(w)) pendingSaveList.push(w); }); if (saveDelayTimer) { clearTimeout(saveDelayTimer); } saveDelayTimer = setTimeout( () => { logger.log("save delay expired."); //pandingSaveList will be cleared soon. let scene = pendingSaveList[0].frameInfo.scene; doSaveWorldList(pendingSaveList, () => { editor.header.updateModifiedStatus(); checkScene(scene); }); //reset saveDelayTimer = null; pendingSaveList = []; }, 500 ); } function doSaveWorldList(worldList, done) { if (worldList.length > 0) { if (worldList[0].data.cfg.disableLabels) { console.log("labels not loaded, save action is prohibitted."); return; } } let ann = worldList.map((w) => { return { frameId: w.frameInfo.frame?.frameId??w.frameInfo.frame, label: JSON.stringify(w.annotation.toBoxAnnotations()), }; }); console.log(ann,'ann') var xhr = new XMLHttpRequest(); xhr.open("POST", http.requestHttp + "/project/frameSave", true); xhr.setRequestHeader("Content-Type","application/json") xhr.setRequestHeader('authorization', http.token); xhr.onreadystatechange = function () { if (this.readyState != 4) return; if (this.status == 200) { document.getElementById('saveSuccess').style.display = "block"; document.getElementById('saveSuccess').innerText = "保存成功!"; let timeout = setTimeout(() => { document.getElementById('saveSuccess').style.display = "none"; clearTimeout(timeout); }, 3000); worldList.forEach((w) => { w.annotation.resetModified(); }); logger.log( `saved: ${worldList[0].frameInfo.scene}: ${worldList.reduce( (a, b) => a + " " + b.frameInfo.frame, "" )}` ); if (done) { done(); } } else { window.editor.infoBox.show( "Error", `save failed, status : ${this.status}` ); } }; const uniqueArray = deduplicatePreferString(ann) xhr.send(JSON.stringify({frameSaveDTOList:uniqueArray})); } function deduplicatePreferString(array) { const map = new Map(); array.forEach(item => { const key = String(item.id); // 如果还没有这个key,或者已存在的不是string类型而当前的是string类型 if (!map.has(key) || (typeof map.get(key).id !== 'string' && typeof item.id === 'string')) { map.set(key, item); } }); return Array.from(map.values()); } // function saveWorld(world, done){ // if (world.data.cfg.disableLabels){ // logger.log("labels not loaded, save action is prohibitted.") // return; // } // console.log(world.annotation.boxes.length, "boxes"); // let bbox_annotations = world.annotation.toBoxAnnotations(); // var xhr = new XMLHttpRequest(); // xhr.open("POST", "/saveworld" +"?scene="+world.frameInfo.scene+"&frame="+world.frameInfo.frame, true); // xhr.setRequestHeader('Content-Type', 'application/json'); // xhr.onreadystatechange = function () { // if (this.readyState != 4) return; // if (this.status == 200) { // logger.log(`saved: ${world}`); // world.annotation.resetModified(); // //reload obj-ids of the scene // //todo: this shall be moved to done // //load_obj_ids_of_scene(world.frameInfo.scene); // if(done){ // done(); // } // } // // end of state change: it can be after some time (async) // }; // var b = JSON.stringify(bbox_annotations); // //console.log(b); // xhr.send(b); // } export { saveWorldList, reloadWorldList };