Files
teamat/studio/app.html
T

546 lines
23 KiB
HTML

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>한땀 작업실</title>
<style>
:root {
--paper: #f7f4ee;
--card: #fffdf9;
--ink: #1f1c18;
--muted: #7a726a;
--line: #ddd5c9;
--accent: #7c5c3e;
--ok: #2f6b47;
--warn: #a4471f;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
margin: 0;
background: var(--paper);
color: var(--ink);
font: 15px/1.55 -apple-system, "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
display: flex;
flex-direction: column;
}
header.top {
display: flex;
align-items: baseline;
gap: 14px;
padding: 14px 20px;
border-bottom: 1px solid var(--line);
background: var(--card);
}
header.top h1 { font-size: 17px; margin: 0; letter-spacing: .08em; }
header.top .meta { color: var(--muted); font-size: 12.5px; }
header.top .spacer { flex: 1; }
main.split { display: flex; flex: 1; min-height: 0; }
/* ---------- 목록 ---------- */
aside.list {
width: 330px;
border-right: 1px solid var(--line);
display: flex;
flex-direction: column;
min-height: 0;
background: var(--card);
}
.search-wrap { padding: 12px 12px 8px; }
input[type=search], input[type=text], input[type=number], input[type=date], input[type=url], textarea, select {
width: 100%;
font: inherit;
color: inherit;
background: #fff;
border: 1px solid var(--line);
border-radius: 4px;
padding: 7px 9px;
}
input[type=search] { font-size: 14px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 12px 8px; }
.chip {
font-size: 12px;
padding: 3px 9px;
border: 1px solid var(--line);
border-radius: 999px;
background: #fff;
color: var(--muted);
cursor: pointer;
}
.chip[aria-pressed=true] { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.listbar { display: flex; gap: 8px; align-items: center; padding: 0 12px 8px; }
.listbar select { width: auto; font-size: 12.5px; padding: 4px 6px; }
.listbar .count { font-size: 12px; color: var(--muted); margin-left: auto; }
ul.rows { list-style: none; margin: 0; padding: 0 6px 24px; overflow-y: auto; flex: 1; }
ul.rows li { padding: 0; margin: 0 0 2px; }
ul.rows button.row {
display: flex;
gap: 9px;
align-items: center;
width: 100%;
text-align: left;
background: none;
border: 0;
border-radius: 4px;
padding: 7px 8px;
cursor: pointer;
font: inherit;
color: inherit;
}
ul.rows button.row:hover { background: #f1ece2; }
ul.rows button.row[aria-current=true] { background: #ece4d5; outline: 1px solid var(--accent); }
.thumb {
width: 40px; height: 40px; flex: 0 0 40px;
object-fit: cover; border-radius: 3px;
background: #eee; border: 1px solid var(--line);
}
.row-main { min-width: 0; flex: 1; }
.row-title { font-size: 13.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge {
font-size: 10.5px; padding: 1px 6px; border-radius: 3px;
border: 1px solid var(--line); color: var(--muted); white-space: nowrap;
}
.badge.sold { color: #fff; background: var(--warn); border-color: var(--warn); }
.badge.feat { color: var(--accent); border-color: var(--accent); }
.empty { padding: 20px 14px; color: var(--muted); font-size: 13px; }
/* ---------- 편집기 ---------- */
section.editor { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.editor-head {
display: flex; align-items: center; gap: 10px;
padding: 12px 20px; border-bottom: 1px solid var(--line);
}
.editor-head h2 { font-size: 15px; margin: 0; }
.editor-head code { font-size: 12px; color: var(--muted); background: #f2ece1; padding: 2px 6px; border-radius: 3px; }
.editor-body { overflow-y: auto; padding: 18px 20px 28px; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; }
.grid .wide { grid-column: 1 / -1; }
label.field { display: block; }
label.field > span.lbl { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
label.field input[type=checkbox] { width: 15px; height: 15px; margin-right: 6px; vertical-align: -2px; }
.check-line { display: flex; align-items: center; gap: 4px; padding-top: 19px; }
textarea { resize: vertical; min-height: 90px; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 13px; line-height: 1.6; }
.img-line { display: flex; gap: 8px; align-items: center; }
.img-line input { flex: 1; }
.img-preview { width: 52px; height: 52px; object-fit: cover; border: 1px solid var(--line); border-radius: 3px; background: #eee; }
.drop-hint { font-size: 11.5px; color: var(--muted); margin-top: 4px; }
.drop-hint.hot { color: var(--accent); font-weight: 600; }
.divider { height: 1px; background: var(--line); margin: 18px 0 14px; }
.section-label { font-size: 11.5px; letter-spacing: .12em; color: var(--muted); margin-bottom: 6px; }
footer.savebar {
display: flex; align-items: center; gap: 10px;
padding: 11px 20px; border-top: 1px solid var(--line); background: var(--card);
}
footer.savebar .state { font-size: 12.5px; color: var(--muted); }
footer.savebar .spacer { flex: 1; }
button {
font: inherit; cursor: pointer; border-radius: 4px;
border: 1px solid var(--line); background: #fff; color: inherit; padding: 7px 13px;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover:not(:disabled) { background: #6a4d33; }
button:disabled { opacity: .45; cursor: default; }
button.ghost { background: none; border-color: transparent; color: var(--muted); }
button.ghost:hover { color: var(--accent); }
/* ---------- 이력 서랍 ---------- */
#drawer {
position: fixed; inset: 0; background: rgba(31, 28, 24, .28);
display: none; justify-content: flex-end; z-index: 40;
}
#drawer.open { display: flex; }
.drawer-panel {
width: min(460px, 92vw); background: var(--card); height: 100%;
border-left: 1px solid var(--line); display: flex; flex-direction: column;
}
.drawer-panel header {
display: flex; align-items: center; gap: 10px;
padding: 13px 16px; border-bottom: 1px solid var(--line);
}
.drawer-panel header h3 { font-size: 14px; margin: 0; }
.drawer-panel header .spacer { flex: 1; }
ul.commits { list-style: none; margin: 0; padding: 6px; overflow-y: auto; }
ul.commits li {
display: flex; gap: 10px; align-items: center;
padding: 8px 10px; border-bottom: 1px solid #f0eae0; font-size: 13px;
}
ul.commits .sha { font-family: ui-monospace, Menlo, monospace; font-size: 11.5px; color: var(--muted); }
ul.commits .subject { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
ul.commits .when { font-size: 11px; color: var(--muted); }
ul.commits button { padding: 3px 9px; font-size: 12px; }
/* ---------- 토스트 ---------- */
#toast {
position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%) translateY(20px);
background: #241f19; color: #f7f4ee; padding: 10px 16px; border-radius: 5px;
font-size: 13px; max-width: min(720px, 92vw); opacity: 0; pointer-events: none;
transition: opacity .18s, transform .18s; z-index: 60; white-space: pre-wrap;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.err { background: #6d2410; }
#toast.ok { background: #1f4a33; }
.hint { font-size: 12px; color: var(--muted); }
kbd {
font: 11px ui-monospace, Menlo, monospace; border: 1px solid var(--line);
border-bottom-width: 2px; border-radius: 3px; padding: 0 4px; background: #fff;
}
</style>
</head>
<body>
<header class="top">
<h1>한땀 작업실</h1>
<span class="meta" id="topmeta">불러오는 중…</span>
<span class="spacer"></span>
<span class="hint"><kbd>/</kbd> 검색 · <kbd>↑↓</kbd> 이동 · <kbd>⌘S</kbd> 저장</span>
<button id="btnHistory" class="ghost">이력 / 되돌리기</button>
</header>
<main class="split">
<aside class="list">
<div class="search-wrap">
<input type="search" id="q" placeholder="제목 · 슬러그 · 소재 · 본문 검색" autocomplete="off">
</div>
<div class="chips" id="chips">
<button class="chip" data-f="all" aria-pressed="true">전체</button>
<button class="chip" data-f="on" aria-pressed="false">판매중</button>
<button class="chip" data-f="sold" aria-pressed="false">품절</button>
<button class="chip" data-f="feat" aria-pressed="false">대표작</button>
<button class="chip" data-f="year" aria-pressed="false">올해</button>
</div>
<div class="listbar">
<select id="sort">
<option value="modified">최근 수정</option>
<option value="date">날짜 최신</option>
<option value="title">이름</option>
<option value="price">가격</option>
</select>
<span class="count" id="rowcount"></span>
</div>
<ul class="rows" id="rows"></ul>
</aside>
<section class="editor">
<div class="editor-head">
<h2 id="edTitle">작품을 고르세요</h2>
<code id="edSlug"></code>
<span class="spacer"></span>
<span class="hint" id="edModified"></span>
</div>
<div class="editor-body" id="edBody">
<p class="empty">왼쪽 목록에서 작품을 고르면 여기서 바로 고칠 수 있습니다.</p>
</div>
<footer class="savebar">
<button id="btnSave" class="primary" disabled>저장 (커밋)</button>
<label class="hint" style="display:flex;align-items:center;gap:5px;">
<input type="checkbox" id="deployToo"> 배포까지
</label>
<span class="state" id="state">—</span>
<span class="spacer"></span>
<button id="btnReload" class="ghost" disabled>되돌리기(저장 전)</button>
</footer>
</section>
</main>
<div id="drawer">
<div class="drawer-panel">
<header>
<h3>최근 이력</h3>
<span class="spacer"></span>
<label class="hint" style="display:flex;align-items:center;gap:5px;">
<input type="checkbox" id="revertDeploy"> 되돌린 뒤 배포
</label>
<button class="ghost" id="btnCloseDrawer">닫기</button>
</header>
<ul class="commits" id="commits"></ul>
</div>
</div>
<div id="toast"></div>
<script>
const state = { rows: [], fields: [], current: null, dirty: false, filter: 'all', q: '', sort: 'modified' };
const $ = (id) => document.getElementById(id);
const esc = (s) => String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));
let toastTimer = null;
function toast(msg, kind = '') {
const el = $('toast');
el.textContent = msg;
el.className = 'show ' + kind;
clearTimeout(toastTimer);
toastTimer = setTimeout(() => { el.className = ''; }, kind === 'err' ? 7000 : 3200);
}
async function api(path, opts) {
const res = await fetch(path, opts);
try { return await res.json(); } catch (e) { return { ok: false, error: '응답을 읽을 수 없습니다 (' + res.status + ')' }; }
}
function markDirty(dirty) {
state.dirty = dirty;
$('state').textContent = dirty ? '저장하지 않은 변경' : '변경 없음';
$('btnSave').disabled = !state.current || !dirty;
}
/* ------------------------------------------------------------ 목록 */
function visibleRows() {
const q = state.q.trim().toLowerCase();
let rows = state.rows.filter((r) => {
if (state.filter === 'on' && r.sold_out) return false;
if (state.filter === 'sold' && !r.sold_out) return false;
if (state.filter === 'feat' && !r.featured) return false;
if (state.filter === 'year' && !String(r.date || '').startsWith(String(new Date().getFullYear()))) return false;
if (q && !(r.search || '').includes(q)) return false;
return true;
});
const k = state.sort;
rows.sort((a, b) => {
if (k === 'modified') return (b.mtime || 0) - (a.mtime || 0);
if (k === 'date') return String(b.date).localeCompare(String(a.date));
if (k === 'title') return String(a.title).localeCompare(String(b.title), 'ko');
if (k === 'price') return (b.price || 0) - (a.price || 0);
return 0;
});
return rows;
}
function renderList() {
const rows = visibleRows();
$('rowcount').textContent = rows.length + ' / ' + state.rows.length + '점';
const ul = $('rows');
if (!rows.length) {
ul.innerHTML = '<li class="empty">조건에 맞는 작품이 없습니다.</li>';
return;
}
ul.innerHTML = rows.map((r) => {
const active = state.current === r.slug;
const img = r.image ? '<img class="thumb" src="/media/' + encodeURI(r.image) + '" alt="" loading="lazy">' : '<span class="thumb"></span>';
const badges = [
r.sold_out ? '<span class="badge sold">품절</span>' : '',
r.featured ? '<span class="badge feat">대표</span>' : '',
].join('');
return '<li><button class="row" data-slug="' + esc(r.slug) + '" aria-current="' + active + '">' +
img +
'<span class="row-main"><span class="row-title">' + esc(r.title) + ' ' + badges + '</span>' +
'<span class="row-sub">' + esc(r.slug) + ' · ' + esc(r.date) + ' · ' + esc(r.modified) + '</span></span>' +
'</button></li>';
}).join('');
}
/* ------------------------------------------------------------ 편집기 */
function fieldHtml(f, value) {
const id = 'f_' + f.key;
const wide = ['image', 'image_alt', 'care', 'naver_url'].includes(f.key) ? ' wide' : '';
if (f.type === 'bool') {
return '<label class="field' + wide + '"><span class="check-line"><input type="checkbox" id="' + id + '" data-key="' + f.key + '"' +
(value ? ' checked' : '') + '> ' + esc(f.label) + '</span></label>';
}
if (f.type === 'lines') {
return '<label class="field wide"><span class="lbl">' + esc(f.label) + '</span>' +
'<textarea id="' + id + '" data-key="' + f.key + '" rows="3">' + esc(value) + '</textarea></label>';
}
if (f.type === 'image') {
return '<label class="field wide"><span class="lbl">' + esc(f.label) + '</span>' +
'<span class="img-line"><input type="text" id="' + id + '" data-key="' + f.key + '" value="' + esc(value) + '">' +
'<img class="img-preview" id="' + id + '_prev" src="' + (value ? '/media/' + encodeURI(value) : '') + '" alt="">' +
'<button type="button" class="ghost" id="' + id + '_pick">올리기</button></span>' +
'<span class="drop-hint" id="' + id + '_hint">JPG를 여기로 끌어다 놓아도 됩니다 (assets/img 에 저장)</span></label>';
}
const type = f.type === 'number' ? 'number' : (f.type === 'date' ? 'date' : (f.type === 'url' ? 'url' : 'text'));
return '<label class="field' + wide + '"><span class="lbl">' + esc(f.label) + '</span>' +
'<input type="' + type + '" id="' + id + '" data-key="' + f.key + '" value="' + esc(value) + '"></label>';
}
function renderEditor(p) {
const known = new Set(state.fields.map((f) => f.key));
const grid = state.fields.map((f) => fieldHtml(f, p.fields[f.key])).join('');
const extra = p.extra_keys.length
? '<p class="hint">이 파일에만 있는 키(보존됨): ' + p.extra_keys.map(esc).join(', ') + '</p>'
: '';
$('edBody').innerHTML =
'<div class="grid">' + grid + '</div>' +
'<div class="divider"></div>' +
'<div class="section-label">본문 (마크다운)</div>' +
'<textarea id="body" rows="14" style="min-height:240px">' + esc(p.body) + '</textarea>' +
'<p class="hint" style="margin-top:8px">문단은 빈 줄로 나눕니다. <code>## 제목</code>, <code>- 목록</code>, <code>![설명](assets/img/파일.jpg)</code> 지원.</p>' +
'<div style="margin-top:10px">' + extra + '</div>';
$('edTitle').textContent = p.fields.title || p.slug;
$('edSlug').textContent = p.slug;
$('edModified').textContent = '파일 수정 ' + p.modified;
$('btnReload').disabled = false;
wireEditor();
markDirty(false);
}
function wireEditor() {
document.querySelectorAll('[data-key]').forEach((el) => {
el.addEventListener('input', () => markDirty(true));
el.addEventListener('change', () => markDirty(true));
});
const img = $('f_image');
if (img) {
img.addEventListener('input', () => { $('f_image_prev').src = img.value ? '/media/' + encodeURI(img.value) : ''; });
$('f_image_pick').addEventListener('click', () => pickImage(img));
const hint = $('f_image_hint');
['dragenter', 'dragover'].forEach((ev) => hint.addEventListener(ev, (e) => { e.preventDefault(); hint.classList.add('hot'); }));
['dragleave', 'drop'].forEach((ev) => hint.addEventListener(ev, () => hint.classList.remove('hot')));
hint.addEventListener('drop', (e) => {
e.preventDefault();
const file = e.dataTransfer.files && e.dataTransfer.files[0];
if (file) uploadImage(file, img);
});
}
}
function pickImage(target) {
const input = document.createElement('input');
input.type = 'file';
input.accept = 'image/jpeg';
input.addEventListener('change', () => { if (input.files[0]) uploadImage(input.files[0], target); });
input.click();
}
async function uploadImage(file, target) {
$('state').textContent = '이미지 올리는 중…';
const res = await fetch('/api/upload?name=' + encodeURIComponent(file.name), {
method: 'POST', headers: { 'Content-Type': 'application/octet-stream' }, body: file,
});
const data = await res.json().catch(() => ({ ok: false, error: '업로드 실패' }));
if (!data.ok) { toast(data.error || '업로드 실패', 'err'); $('state').textContent = '—'; return; }
target.value = data.path;
target.dispatchEvent(new Event('input', { bubbles: true }));
target.dispatchEvent(new Event('change', { bubbles: true }));
toast('이미지 저장: ' + data.path, 'ok');
}
/* ------------------------------------------------------------ 열기/저장 */
async function openProduct(slug) {
if (state.dirty && !confirm('저장하지 않은 변경이 있습니다. 버리고 넘어갈까요?')) return;
const data = await api('/api/product?slug=' + encodeURIComponent(slug));
if (!data.ok) { toast(data.error || '작품을 열 수 없습니다', 'err'); return; }
state.current = slug;
renderList();
renderEditor(data.product);
$('state').textContent = '변경 없음';
}
function collect() {
const fields = {};
document.querySelectorAll('[data-key]').forEach((el) => {
fields[el.dataset.key] = el.type === 'checkbox' ? el.checked : el.value;
});
return { slug: state.current, fields, body: $('body').value, deploy: $('deployToo').checked };
}
async function save() {
if (!state.current) return;
const payload = collect();
$('btnSave').disabled = true;
$('state').textContent = '저장 중… (빌드 → 커밋)';
const data = await api('/api/product', {
method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload),
});
if (!data.ok) {
$('state').textContent = '실패';
toast((data.error || '저장 실패') + (data.log ? '\n' + data.log : ''), 'err');
$('btnSave').disabled = false;
return;
}
markDirty(false);
const deployNote = data.deploy ? (data.deploy.ok ? ' / 배포 OK' : ' / 배포 실패') : '';
toast(data.message + (data.commit ? ' · ' + data.commit : '') + deployNote + (data.log ? '\n' + data.log : ''), data.deploy && !data.deploy.ok ? 'err' : 'ok');
await refresh({ keep: true });
}
async function refresh(opts = {}) {
const data = await api('/api/state');
if (!data.ok) { toast('상태를 읽지 못했습니다', 'err'); return; }
state.rows = data.products;
state.fields = data.fields;
$('topmeta').textContent = data.count + '점 · HEAD ' + data.head + ' · ' + data.repo;
renderList();
if (!opts.keep) {
const wanted = new URLSearchParams(location.search).get('slug');
if (wanted) openProduct(wanted);
}
}
/* ------------------------------------------------------------ 이력 */
async function openHistory() {
const data = await api('/api/history');
if (!data.ok) { toast('이력을 읽지 못했습니다', 'err'); return; }
$('commits').innerHTML = data.history.map((c) =>
'<li><span class="sha">' + esc(c.sha) + '</span>' +
'<span class="subject" title="' + esc(c.subject) + '">' + esc(c.subject) + '</span>' +
'<span class="when">' + esc(c.date) + '</span>' +
'<button data-revert="' + esc(c.sha) + '">되돌리기</button></li>'
).join('');
$('drawer').classList.add('open');
}
async function revertTo(sha, btn) {
if (!confirm(sha + ' 커밋을 되돌립니다(새 커밋으로 취소). 계속할까요?')) return;
if (btn) btn.disabled = true;
const data = await api('/api/revert', {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ sha, deploy: $('revertDeploy').checked }),
});
toast(data.ok ? data.message : ((data.error || '실패') + (data.log ? '\n' + data.log : '')), data.ok ? 'ok' : 'err');
if (data.ok) {
await refresh({ keep: true });
if (state.current) { const p = await api('/api/product?slug=' + encodeURIComponent(state.current)); if (p.ok) renderEditor(p.product); }
openHistory();
} else if (btn) btn.disabled = false;
}
/* ------------------------------------------------------------ 이벤트 */
$('q').addEventListener('input', (e) => { state.q = e.target.value; renderList(); });
$('sort').addEventListener('change', (e) => { state.sort = e.target.value; renderList(); });
$('chips').addEventListener('click', (e) => {
const chip = e.target.closest('.chip');
if (!chip) return;
state.filter = chip.dataset.f;
document.querySelectorAll('.chip').forEach((c) => c.setAttribute('aria-pressed', String(c === chip)));
renderList();
});
$('rows').addEventListener('click', (e) => {
const btn = e.target.closest('button.row');
if (btn) openProduct(btn.dataset.slug);
});
$('btnSave').addEventListener('click', save);
$('btnReload').addEventListener('click', () => { if (state.current) openProduct(state.current); });
$('btnHistory').addEventListener('click', openHistory);
$('btnCloseDrawer').addEventListener('click', () => $('drawer').classList.remove('open'));
$('drawer').addEventListener('click', (e) => { if (e.target.id === 'drawer') $('drawer').classList.remove('open'); });
$('commits').addEventListener('click', (e) => {
const btn = e.target.closest('button[data-revert]');
if (btn) revertTo(btn.dataset.revert, btn);
});
document.addEventListener('keydown', (e) => {
const typing = /^(INPUT|TEXTAREA|SELECT)$/.test(document.activeElement.tagName);
if (e.key === '/' && !typing) { e.preventDefault(); $('q').focus(); return; }
if (e.key === 'Escape') { $('drawer').classList.remove('open'); return; }
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 's') { e.preventDefault(); save(); return; }
if (!typing && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {
const rows = visibleRows();
if (!rows.length) return;
e.preventDefault();
let idx = rows.findIndex((r) => r.slug === state.current);
idx = e.key === 'ArrowDown' ? Math.min(rows.length - 1, idx + 1) : Math.max(0, idx - 1);
openProduct(rows[idx].slug);
const el = document.querySelector('button.row[data-slug="' + rows[idx].slug + '"]');
if (el) el.scrollIntoView({ block: 'nearest' });
}
});
window.addEventListener('beforeunload', (e) => { if (state.dirty) { e.preventDefault(); e.returnValue = ''; } });
refresh();
</script>
</body>
</html>