Skip to content

Commit

Permalink
feat: とりあえず履歴欄に追加
Browse files Browse the repository at this point in the history
  • Loading branch information
haruyan-hopemucci committed Feb 18, 2024
1 parent df13227 commit 4875752
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions docs/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const loadHistories = () => {
const imgHistoryKeys = getHistoryKeys()
const container = document.querySelector('#history-container')
const template = document.querySelector('#history-template')
container.childNodes.forEach(node => node.remove())
container.innerHTML = ''
imgHistoryKeys.forEach(item => {
const t = template.content.cloneNode(true)
t.id = item
Expand All @@ -38,6 +38,7 @@ const addHistory = (dataUrl) => {
const imgHistoryKeys = getHistoryKeys()
while (imgHistoryKeys.length > MAX_HISTORY_NUM) {
localStorage.removeItem(imgHistoryKeys[0])
imgHistoryKeys.shift()
}
}

Expand All @@ -63,6 +64,11 @@ $(function () {
}
});

$(".history-item").on("click", "img", (event) => {
gPastedImage.src = event.delegateTarget.querySelector('img').src
redrawLgtnImage()
})

document.addEventListener("paste", (event) => {
event.preventDefault();

Expand Down Expand Up @@ -95,7 +101,7 @@ $(function () {
const conversionResult = await heic2any({ blob: imageFile })
const dataUrl = URL.createObjectURL(conversionResult)
const imgEl = document.querySelector("#pasted-image");
imgEl.addEventListener("load", drawCanvas);
imgEl.addEventListener("load", () => drawCanvas());
imgEl.src = dataUrl;
}

Expand All @@ -108,7 +114,7 @@ $(function () {
imgEl.src = base64;
};
fr.readAsDataURL(imageFile);
imgEl.addEventListener("load", drawCanvas);
imgEl.addEventListener("load", () => drawCanvas());
};

const drawCanvas = function (redraw = false) {
Expand Down Expand Up @@ -140,7 +146,7 @@ $(function () {
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(imgEl, drawX, drawY, drawWidth, drawHeight);
// 再描画の場合は履歴に追加しない
redraw && addHistory(canvas.toDataURL())
!redraw && addHistory(canvas.toDataURL())
context.drawImage(lgtnEl, drawTextX, drawTextY, canvas.width, canvas.height);
copyImageToClipboard(canvas);
};
Expand All @@ -155,6 +161,8 @@ $(function () {
setMessage(
`クリップボードに${selectedOverlayImageValue()}画像がコピーされました!`
);
// 履歴を再読み込み
loadHistories()
})
.catch((ex) => {
console.error(ex);
Expand All @@ -164,6 +172,6 @@ $(function () {
};

const redrawLgtnImage = () => {
drawCanvas(redraw = true);
drawCanvas(true);
};
});

0 comments on commit 4875752

Please sign in to comment.