From afad7c52b39cc117b082ad85e1a4e912999ed8c2 Mon Sep 17 00:00:00 2001 From: Loner Date: Mon, 8 Jan 2024 10:29:20 +0800 Subject: [PATCH] add delete --- web/scripts/app.js | 50 ++------------------------------------------ web/scripts/ui.js | 35 +++++++++++++++++++++---------- web/scripts/utils.js | 50 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 59 deletions(-) diff --git a/web/scripts/app.js b/web/scripts/app.js index f778ccf5bcc..8eee959869d 100644 --- a/web/scripts/app.js +++ b/web/scripts/app.js @@ -6,56 +6,10 @@ import { defaultGraph } from "./defaultGraph.js"; import { getPngMetadata, getWebpMetadata, importA1111, getLatentMetadata } from "./pnginfo.js"; import { addDomClippingSetting } from "./domWidget.js"; import { createImageHost, calculateImageGrid } from "./ui/imagePreview.js" - +import { getUserId } from "./utils.js"; +import { getWorkflow } from "./utils.js"; export const ANIM_PREVIEW_WIDGET = "$$comfy_animation_preview" -function setCookie(name, value, days) { - var expires = ""; - if (days) { - var date = new Date(); - date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); - expires = "; expires=" + date.toUTCString(); - } - document.cookie = name + "=" + (value || "") + expires + "; path=/"; -} - -function getCookie(name) { - var nameEQ = name + "="; - var ca = document.cookie.split(';'); - for (var i = 0; i < ca.length; i++) { - var c = ca[i]; - while (c.charAt(0) == ' ') c = c.substring(1, c.length); - if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); - } - return null; -} - -async function getWorkflow() { - let flow_json = null; - const queryString = window.location.search; - const urlParams = new URLSearchParams(queryString); - const workflowId = urlParams.get('workflow'); - if (workflowId){ - await fetch('../workflows/' + workflowId + '/' + workflowId + '.json').then( - response => { - flow_json = response.json() - } - ) - } - return flow_json; -} - -function getUserId() { - var uid = getCookie('uid'); - if (uid == null) { - const queryString = window.location.search; - const urlParams = new URLSearchParams(queryString); - const email = urlParams.get('email'); - uid = prompt("Please enter your nickname \n(less than ten letters)", email ? email.split("@")[0] : "anonymous"); - setCookie('uid', uid, 999); - } - return uid ? uid : "anonymous"; -} function sanitizeNodeName(string) { diff --git a/web/scripts/ui.js b/web/scripts/ui.js index 6513b28cc0a..55be5b228cc 100644 --- a/web/scripts/ui.js +++ b/web/scripts/ui.js @@ -1,4 +1,5 @@ import {api} from "./api.js"; +import { getUserId } from "./utils.js"; export function $el(tag, propsOrChildren, children) { const split = tag.split("."); @@ -455,10 +456,17 @@ class ComfyList { $el("div.comfy-list-items", [ ...(this.#reverse ? items[section].reverse() : items[section]).map((item) => { // Allow items to specify a custom remove action (e.g. for interrupt current prompt) - // const removeAction = item.remove || { - // name: "Delete", - // cb: () => api.deleteItem(this.#type, item.prompt[1]), - // }; + const removeAction = item.remove || { + name: "Delete", + cb: () => { + var uid = getUserId(); + if(uid == item.prompt[5]) { + api.deleteItem(this.#type, item.prompt[1]) + } else { + alert("You can only delete your own prompts.") + } + }, + }; return $el("div", {textContent: item.prompt[0] + " - " + (item.prompt[5].length <= 10 ? item.prompt[5] : item.prompt[5].substring(0, 10) + "...") + ": "}, [ $el("button", { textContent: "Load", @@ -469,13 +477,18 @@ class ComfyList { } }, }), - // $el("button", { - // textContent: removeAction.name, - // onclick: async () => { - // await removeAction.cb(); - // await this.update(); - // }, - // }), + $el("button", { + textContent: removeAction.name, + onclick: async () => { + var uid = getUserId(); + if(uid == item.prompt[5]) { + await removeAction.cb(); + await this.update(); + } else { + alert("You can only delete your own prompts.") + } + }, + }), ]); }), ]), diff --git a/web/scripts/utils.js b/web/scripts/utils.js index 401aca9e487..b18817c3cd5 100644 --- a/web/scripts/utils.js +++ b/web/scripts/utils.js @@ -65,3 +65,53 @@ export function applyTextReplacements(app, value) { return ((widget.value ?? "") + "").replaceAll(/\/|\\/g, "_"); }); } + + + +function setCookie(name, value, days) { + var expires = ""; + if (days) { + var date = new Date(); + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); + expires = "; expires=" + date.toUTCString(); + } + document.cookie = name + "=" + (value || "") + expires + "; path=/"; +} + +function getCookie(name) { + var nameEQ = name + "="; + var ca = document.cookie.split(';'); + for (var i = 0; i < ca.length; i++) { + var c = ca[i]; + while (c.charAt(0) == ' ') c = c.substring(1, c.length); + if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); + } + return null; +} + +export async function getWorkflow() { + let flow_json = null; + const queryString = window.location.search; + const urlParams = new URLSearchParams(queryString); + const workflowId = urlParams.get('workflow'); + if (workflowId){ + await fetch('../workflows/' + workflowId + '/' + workflowId + '.json').then( + response => { + flow_json = response.json() + } + ) + } + return flow_json; +} + +export function getUserId() { + var uid = getCookie('uid'); + if (uid == null) { + const queryString = window.location.search; + const urlParams = new URLSearchParams(queryString); + const email = urlParams.get('email'); + uid = prompt("Please enter your nickname \n(less than ten letters)", email ? email.split("@")[0] : "anonymous"); + setCookie('uid', uid, 999); + } + return uid ? uid : "anonymous"; +} \ No newline at end of file