fix(project): 설명란 이미지 복사 — Ctrl+C·보기 창 [복사] 로 PNG 그림만 클립보드에
- 원인: 설명란 이미지 "선택"은 편집기 표시라 기본 Ctrl+C 로는 아무것도 복사되지 않았고, 브라우저 "이미지 복사"는 <img src=첨부 주소> HTML 을 같이 넣어 로그인 없는 프로그램(카카오톡·한글 등)엔 빈칸 - copyImageToClipboard: 캔버스로 PNG 변환 → navigator.clipboard.write(ClipboardItem, Promise) - 설명란 고른 이미지 Ctrl+C(한글 입력 상태 e.code), 글자를 골라 둔 상태면 기본 복사 그대로 - 이미지 보기 창: 아래 막대 [복사] 버튼 + Ctrl+C, 위쪽 알림 - tests/js/image_copy.browser.test.js (Edge 로 9항목 통과), project.js 버전 20261015a, 도움말·문서 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+60
-3
@@ -2305,6 +2305,35 @@
|
||||
return false;
|
||||
}
|
||||
|
||||
// ── 이미지 → 클립보드(PNG 그림만, 2026-10-15) — 브라우저 기본 복사는 `<img src=첨부 주소>` HTML 을 같이(또는 그것만) 넣는데,
|
||||
// 첨부 주소는 로그인해야 받을 수 있어서 카카오톡·한글·워드처럼 HTML 을 먼저 쓰는 프로그램에선 빈칸이 된다.
|
||||
// 그래서 화면의 이미지를 캔버스로 PNG 로 만들어 그림만 넣는다(같은 서버 이미지라 캔버스가 오염되지 않음).
|
||||
// ClipboardItem 에 Promise 를 넘겨야 변환을 기다리는 동안에도 키·클릭 직후 권한이 유지된다.
|
||||
function imagePngBlob(img) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
function draw() {
|
||||
try {
|
||||
const c = document.createElement("canvas");
|
||||
c.width = img.naturalWidth; c.height = img.naturalHeight;
|
||||
c.getContext("2d").drawImage(img, 0, 0);
|
||||
c.toBlob(function (b) { if (b) resolve(b); else reject(new Error("변환 실패")); }, "image/png");
|
||||
} catch (e) { reject(e); }
|
||||
}
|
||||
if (img.complete && img.naturalWidth) { draw(); return; }
|
||||
img.addEventListener("load", draw, { once: true });
|
||||
img.addEventListener("error", function () { reject(new Error("이미지를 불러오지 못했습니다.")); }, { once: true });
|
||||
});
|
||||
}
|
||||
function copyImageToClipboard(img) {
|
||||
if (!navigator.clipboard || !navigator.clipboard.write || !window.ClipboardItem) {
|
||||
showToast("이 브라우저는 이미지 복사를 지원하지 않습니다. 이미지를 더블클릭해 연 창에서 복사해 주세요.", "error");
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
return navigator.clipboard.write([new ClipboardItem({ "image/png": imagePngBlob(img) })])
|
||||
.then(function () { showToast("이미지를 복사했습니다."); return true; })
|
||||
.catch(function (e) { showToast("이미지 복사 실패: " + ((e && e.message) || e), "error"); return false; });
|
||||
}
|
||||
|
||||
// ── 이미지 보기 창 — 설명란 이미지를 더블클릭하면 별도 브라우저 창(팝업 창, 페이지 안 팝업 아님)으로 연다.
|
||||
// 창 크기 = 이미지 원본 크기(+ 여백·도구 막대), 화면보다 크면 화면 95% 까지. 처음엔 원본(100%), 창보다 크면 창에 맞춤.
|
||||
// 창 안: 휠 = 마우스 위치 기준 확대·축소, 끌기 = 이동, 아래 막대 − / % / + · 1:1(원본) · 맞춤,
|
||||
@@ -2337,12 +2366,19 @@
|
||||
'#pct{min-width:56px;text-align:center;font-size:13px;font-variant-numeric:tabular-nums}' +
|
||||
'.sep{width:1px;height:18px;background:rgba(255,255,255,.18);margin:0 4px}' +
|
||||
'#err{position:fixed;inset:0;display:none;align-items:center;justify-content:center;color:#cfd3da;font-size:14px}' +
|
||||
'#msg{position:fixed;left:50%;top:14px;transform:translate(-50%,-8px);padding:7px 14px;border-radius:8px;background:rgba(40,42,47,.95);' +
|
||||
'border:1px solid rgba(255,255,255,.1);font-size:13px;opacity:0;pointer-events:none;transition:opacity .18s,transform .18s;z-index:3}' +
|
||||
'#msg.show{opacity:1;transform:translate(-50%,0)}#msg.bad{color:#ffb4a8}' +
|
||||
'</style></head><body><div id="stage"><img id="img" alt="" draggable="false"></div><div id="err">이미지를 불러오지 못했습니다.</div>' +
|
||||
'<div id="msg" role="status" aria-live="polite"></div>' +
|
||||
'<div id="bar">' +
|
||||
'<button id="out" title="축소 (−)" aria-label="축소">' + ICON('<path d="M5 10h10"/>') + "</button>" +
|
||||
'<span id="pct">100%</span>' +
|
||||
'<button id="in" title="확대 (+)" aria-label="확대">' + ICON('<path d="M5 10h10M10 5v10"/>') + "</button>" +
|
||||
'<span class="sep"></span><button id="one" title="원본 크기 (1)">1:1</button><button id="fit" title="창에 맞춤 (0)">맞춤</button></div>' +
|
||||
'<span class="sep"></span><button id="one" title="원본 크기 (1)">1:1</button><button id="fit" title="창에 맞춤 (0)">맞춤</button>' +
|
||||
'<span class="sep"></span><button id="copy" title="이미지 복사 (Ctrl+C) — 카카오톡·한글·그림판 등에 그림으로 붙여넣기" aria-label="이미지 복사">' +
|
||||
ICON('<rect x="7" y="7" width="10" height="10" rx="1.6"/><path d="M13 7V4.6A1.6 1.6 0 0 0 11.4 3H4.6A1.6 1.6 0 0 0 3 4.6v6.8A1.6 1.6 0 0 0 4.6 13H7"/>') +
|
||||
' 복사</button></div>' +
|
||||
"<script>(function(){" +
|
||||
"var SRC=" + JSON.stringify(src) + ",TITLE=" + JSON.stringify(title) + ";document.title=TITLE;" +
|
||||
"var img=document.getElementById('img'),stage=document.getElementById('stage'),pct=document.getElementById('pct');" +
|
||||
@@ -2363,7 +2399,18 @@
|
||||
"document.getElementById('out').onclick=function(){var m=mid();zoomAt(s/1.25,m[0],m[1]);};" +
|
||||
"document.getElementById('one').onclick=function(){centerAt(1);};" +
|
||||
"document.getElementById('fit').onclick=function(){centerAt(fitScale());};" +
|
||||
"addEventListener('keydown',function(e){var m=mid();if(e.key==='+'||e.key==='='){zoomAt(s*1.25,m[0],m[1]);}else if(e.key==='-'||e.key==='_'){zoomAt(s/1.25,m[0],m[1]);}" +
|
||||
// 복사 = PNG 그림만(copyImageToClipboard 와 같은 이유 — 브라우저 기본 "이미지 복사"의 첨부 주소 HTML 은 로그인 없인 안 열림)
|
||||
"var msg=document.getElementById('msg'),mt=0;" +
|
||||
"function note(t,bad){msg.textContent=t;msg.className=bad?'show bad':'show';clearTimeout(mt);mt=setTimeout(function(){msg.className=bad?'bad':'';},1800);}" +
|
||||
"function pngBlob(){return new Promise(function(ok,no){try{var c=document.createElement('canvas');c.width=img.naturalWidth;c.height=img.naturalHeight;" +
|
||||
"c.getContext('2d').drawImage(img,0,0);c.toBlob(function(b){if(b)ok(b);else no(new Error('변환 실패'));},'image/png');}catch(e){no(e);}});}" +
|
||||
"function copyImg(){if(!img.naturalWidth)return;" +
|
||||
"if(!navigator.clipboard||!navigator.clipboard.write||!window.ClipboardItem){note('이 브라우저는 이미지 복사를 지원하지 않습니다.',1);return;}" +
|
||||
"navigator.clipboard.write([new ClipboardItem({'image/png':pngBlob()})]).then(function(){note('이미지를 복사했습니다.');}," +
|
||||
"function(e){note('복사 실패: '+((e&&e.message)||e),1);});}" +
|
||||
"document.getElementById('copy').onclick=copyImg;" +
|
||||
"addEventListener('keydown',function(e){if((e.ctrlKey||e.metaKey)&&!e.shiftKey&&!e.altKey&&(e.code==='KeyC'||e.key==='c'||e.key==='C')){e.preventDefault();copyImg();return;}" +
|
||||
"var m=mid();if(e.key==='+'||e.key==='='){zoomAt(s*1.25,m[0],m[1]);}else if(e.key==='-'||e.key==='_'){zoomAt(s/1.25,m[0],m[1]);}" +
|
||||
"else if(e.key==='0'){centerAt(fitScale());}else if(e.key==='1'){centerAt(1);}else if(e.key==='Escape'){window.close();}});" +
|
||||
"})();<\/script></body></html>";
|
||||
win.document.open();
|
||||
@@ -8451,9 +8498,19 @@
|
||||
document.addEventListener("mouseup", function () { dragging = false; });
|
||||
document.addEventListener("keydown", function (e) {
|
||||
if (!selectedImg) return;
|
||||
if (e.key !== "Delete" && e.key !== "Backspace") return;
|
||||
const active = document.activeElement;
|
||||
if (active && ["INPUT", "TEXTAREA", "SELECT"].indexOf(active.tagName) >= 0) return;
|
||||
// 고른 이미지 Ctrl+C = PNG 그림으로 복사(copyImageToClipboard). 이미지 선택은 브라우저 선택이 아니라
|
||||
// 이 편집기의 표시라 기본 Ctrl+C 로는 아무것도 복사되지 않았다. 글자를 드래그해 둔 상태면 글자 복사 그대로.
|
||||
// e.code — 한글 입력 상태에선 e.key 가 "ㅊ" 로 온다.
|
||||
if ((e.ctrlKey || e.metaKey) && !e.shiftKey && !e.altKey && (e.code === "KeyC" || e.key === "c" || e.key === "C")) {
|
||||
const sel = window.getSelection && window.getSelection();
|
||||
if (sel && !sel.isCollapsed && String(sel).trim()) return;
|
||||
e.preventDefault();
|
||||
copyImageToClipboard(selectedImg);
|
||||
return;
|
||||
}
|
||||
if (e.key !== "Delete" && e.key !== "Backspace") return;
|
||||
e.preventDefault();
|
||||
deleteSelectedImage();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user