Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
SunWuyuan committed Jan 21, 2025
1 parent 2e116d2 commit 8f72b3f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
13 changes: 8 additions & 5 deletions routes/router_project.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,16 +364,16 @@ router.get("/:id/:branch/:ref", async (req, res, next) => {
}

const accessFileToken = await generateFileAccessToken(
defaultSource.sha256,
defaultSource,
userId
);

return res.status(200).send({
status: "success",
message: "获取成功",
status: "error",
message: "未初始化的作品",
commit: {
commit_file: defaultSource.sha256,
commit_message: "默认作品",
commit_file: defaultSource,
commit_message: "NoFirstCommit",
commit_date: new Date(),
},
accessFileToken,
Expand Down Expand Up @@ -484,6 +484,9 @@ router.post("/:id/init", needlogin, async (req, res, next) => {
.send({ status: "error", message: "项目已存在提交或不存在" });
}

const project = await prisma.ow_projects.findFirst({
where: { id: Number(id) },
});
// 获取默认作品
const defaultSource = default_project[project.type];
if (!defaultSource) {
Expand Down
48 changes: 40 additions & 8 deletions views/scratchtool.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const isEmbed = !document.getElementById("zerocattool");

// 加载外部资源
const loadScript = (src, callback) => {
Expand Down Expand Up @@ -174,17 +173,17 @@ function showLoginButton() {
}

function renderButtons() {
const zerocatool = document.getElementById("zerocatool");
zerocatool.innerHTML = ""; // 清空现有按钮
const zerocattool = document.getElementById("zerocattool");
zerocattool.innerHTML = ""; // 清空现有按钮
buttons.forEach((button) => {
const btnTemplate = zerocatool.cloneNode(true);
const btnTemplate = zerocattool.cloneNode(true);
btnTemplate.id = button.id;
btnTemplate.innerHTML = button.text;
if (button.onclick) btnTemplate.onclick = button.onclick;
btnTemplate.style.display = "inline-block";
zerocatool.parentNode.appendChild(btnTemplate);
zerocattool.parentNode.appendChild(btnTemplate);
});
zerocatool.style.display = "none";
zerocattool.style.display = "none";
}

function setButton(id, text, onclick) {
Expand Down Expand Up @@ -296,10 +295,11 @@ function getZerocatUserInfo(userid) {
return result;
}


let isEmbed
function initialize() {
if (window.initialized) return; // 避免初始化两次
window.initialized = true;
isEmbed = !document.getElementById("zerocattool");

if (!isEmbed) {
const zerocatTabHTML = `
Expand Down Expand Up @@ -440,6 +440,38 @@ function downloadAndLoadProject(pid) {
data: { token: localStorage.getItem("token") },
success: function (response) {
if (response.status === "error") {

if (response.commit.commit_message === "NoFirstCommit") {
mdui.alert({
headline: "作品未初始化",
description: "你可以以Scratch模板初始化此作品",
confirmText: "继续",
onConfirm: () => {
$.ajax({
url: `${ZCApiHost}/project/${pid}/init`,
type: "POST",
data: { token: localStorage.getItem("token") },
success: function (response) {
showSnackbar(response.message);
downloadAndLoadProject(pid);
},
error: function (xhr) {
console.error("Error initializing project:", xhr);
mdui.alert({
headline: "初始化失败",
description: `作品初始化失败: ${xhr.responseText}`,
confirmText: "重新加载",
onConfirm: () => {
window.location.reload();
},
});
},
});
},
})
}


mdui.alert({
headline: "加载失败",
description: response.message,
Expand Down Expand Up @@ -678,7 +710,7 @@ function commitProject() {
contentType: "application/json",
headers: { Authorization: `Bearer ${localStorage.getItem("token")}` },
success: function (commitResponse) {
showSnackbar("提交成功");
showSnackbar(commitResponse.message);
document.getElementById("commit-dialog").open=false;
setButton("save-button", "保存完成", () => openCommitDialog());
logText.innerText += `[${getCurrentTime()}]保存完成\n`;
Expand Down

0 comments on commit 8f72b3f

Please sign in to comment.