'); doc.close(); f.contentWindow.focus(); var sel = f.contentWindow.getSelection(); var r = doc.createRange(); r.selectNodeContents(doc.body); sel.removeAllRanges(); sel.addRange(r); ok = doc.execCommand("copy"); sel.removeAllRanges(); }catch(e){ ok = false; } if(f.parentNode) f.parentNode.removeChild(f); try{ window.focus(); }catch(e2){} return ok; } $("#copyRendered").addEventListener("click", function(){ if(!currentHTML){ say("Nothing to copy yet \u2014 add at least a name or an image.", true); return; } var ok = copyRichViaIframe(currentHTML); if(!ok) try{ var sel = window.getSelection(); var range = document.createRange(); range.selectNodeContents(sigOut); sel.removeAllRanges(); sel.addRange(range); ok = document.execCommand("copy"); sel.removeAllRanges(); }catch(e){ ok = false; } if(!ok && window.ClipboardItem && navigator.clipboard && navigator.clipboard.write){ try{ navigator.clipboard.write([ new window.ClipboardItem({ "text/html": new Blob([currentHTML], { type:"text/html" }), "text/plain": new Blob([sigOut.innerText || ""], { type:"text/plain" }) })]); ok = true; }catch(e2){} } say(ok ? "Signature copied — paste into Gmail or Outlook with Ctrl/Cmd + V." : "Copy blocked by the browser. Use \u201CView generated HTML source\u201D and copy manually.", !ok); }); $("#copyCode").addEventListener("click", function(){ if(!currentHTML){ say("Nothing to copy yet \u2014 add at least a name or an image.", true); return; } var ok = copyText(currentHTML); say(ok ? "HTML code copied to your clipboard." : "Copy blocked by the browser. Select the code manually.", !ok); }); $("#downloadBtn").addEventListener("click", function(){ if(!currentHTML){ say("Nothing to download yet \u2014 add at least a name or an image.", true); return; } var fname = (t(state.name) || "signature").toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,"") + "-signature.html"; var doc = '\n\n\n\n\n\n\n' + ind(currentHTML,0) + '\n\n\n'; try{ var blob = new Blob([doc], { type:"text/html;charset=utf-8" }); var a = document.createElement("a"); var href = URL.createObjectURL(blob); a.href = href; a.download = fname; a.rel = "noopener"; document.body.appendChild(a); a.click(); document.body.removeChild(a); setTimeout(function(){ URL.revokeObjectURL(href); }, 4000); say("Downloaded " + fname); }catch(e){ say("Download blocked here \u2014 copy the HTML code instead.", true); } }); $("#resetBtn").addEventListener("click", function(){ state = JSON.parse(JSON.stringify(DEFAULTS)); applyStateToForm(); render(); say("All fields reset to the JI Design starter values."); }); /* ---------------- presets ---------------- */ function getPresets(){ try{ return JSON.parse(store.get(LS_PRESETS) || "[]") || []; }catch(e){ return []; } } function setPresets(list){ store.set(LS_PRESETS, JSON.stringify(list)); } function drawPresets(){ var host = $("#presetHost"); var list = getPresets(); host.innerHTML = ""; if(!list.length){ var e = document.createElement("div"); e.className = "empty"; e.textContent = "No presets saved yet. Fill in the form, name it above, and save."; host.appendChild(e); return; } var ul = document.createElement("ul"); ul.className = "preset-list"; list.forEach(function(p,i){ var li = document.createElement("li"); var wrapDiv = document.createElement("div"); wrapDiv.style.minWidth = "0"; var nm = document.createElement("div"); nm.className = "pname"; nm.textContent = p.name; var meta = document.createElement("div"); meta.className = "pmeta"; meta.textContent = [ (p.data && p.data.name) || "", (p.data && p.data.email) || "" ].filter(Boolean).join(" \u00B7 ") || "\u2014"; wrapDiv.appendChild(nm); wrapDiv.appendChild(meta); var acts = document.createElement("div"); acts.className = "pacts"; var load = document.createElement("button"); load.className = "btn btn-ghost btn-sm"; load.type = "button"; load.textContent = "Load"; load.setAttribute("data-load", String(i)); var del = document.createElement("button"); del.className = "btn btn-quiet btn-sm"; del.type = "button"; del.textContent = "Delete"; del.setAttribute("data-del", String(i)); acts.appendChild(load); acts.appendChild(del); li.appendChild(wrapDiv); li.appendChild(acts); ul.appendChild(li); }); host.appendChild(ul); } $("#savePreset").addEventListener("click", function(){ var input = $("#presetName"); var nm = t(input.value) || t(state.name); if(!nm){ say("Give the preset a name first.", true); return; } var list = getPresets(); var existing = -1; list.forEach(function(p,i){ if(p.name.toLowerCase() === nm.toLowerCase()) existing = i; }); var entry = { name:nm, data:JSON.parse(JSON.stringify(state)), saved:Date.now() }; if(existing >= 0){ list[existing] = entry; } else { list.push(entry); } setPresets(list); input.value = ""; drawPresets(); say(existing >= 0 ? 'Preset "' + nm + '" updated.' : 'Preset "' + nm + '" saved.'); }); $("#presetHost").addEventListener("click", function(ev){ var el = ev.target; if(!el || el.tagName !== "BUTTON") return; var li = el.getAttribute("data-load"), di = el.getAttribute("data-del"); var list = getPresets(); if(li !== null){ var p = list[parseInt(li,10)]; if(!p) return; state = Object.assign({}, DEFAULTS, p.data || {}); applyStateToForm(); render(); say('Loaded preset "' + p.name + '".'); } else if(di !== null){ var idx = parseInt(di,10); var gone = list[idx]; if(!gone) return; list.splice(idx,1); setPresets(list); drawPresets(); say('Preset "' + gone.name + '" deleted.'); } }); /* ---------------- init ---------------- */ (function init(){ var saved = null; try{ saved = JSON.parse(store.get(LS_STATE) || "null"); }catch(e){ saved = null; } state = Object.assign({}, DEFAULTS, saved && typeof saved === "object" ? saved : {}); applyStateToForm(); drawPresets(); render(); })(); })();