初始化版本
This commit is contained in:
201
assets/app.js
Normal file
201
assets/app.js
Normal file
@@ -0,0 +1,201 @@
|
||||
const $ = (sel) => document.querySelector(sel);
|
||||
const $$ = (sel) => Array.from(document.querySelectorAll(sel));
|
||||
|
||||
const state = {
|
||||
currentId: null,
|
||||
list: [],
|
||||
dirty: false,
|
||||
};
|
||||
let weEditor = null;
|
||||
|
||||
function setStatus(text) {
|
||||
const el = $('#status');
|
||||
el.textContent = text || '';
|
||||
}
|
||||
|
||||
function renderList() {
|
||||
const ul = $('#doc-list');
|
||||
ul.innerHTML = '';
|
||||
state.list.forEach((d) => {
|
||||
const li = document.createElement('li');
|
||||
li.className = 'doc-item' + (d.id === state.currentId ? ' active' : '');
|
||||
li.textContent = d.title || '(未命名)';
|
||||
li.onclick = () => loadDoc(d.id);
|
||||
ul.appendChild(li);
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchJSON(url, opts = {}) {
|
||||
const res = await fetch(url, opts);
|
||||
if (!res.ok) throw new Error('请求失败: ' + res.status);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async function refreshList() {
|
||||
const data = await fetchJSON('api.php?action=list');
|
||||
state.list = data.docs || [];
|
||||
renderList();
|
||||
}
|
||||
|
||||
async function loadDoc(id) {
|
||||
const data = await fetchJSON('api.php?action=get&id=' + encodeURIComponent(id));
|
||||
state.currentId = data.id;
|
||||
$('#title-input').value = data.title || '';
|
||||
if (weEditor) weEditor.setHtml(data.content || '');
|
||||
$('#btn-save').disabled = true;
|
||||
$('#btn-delete').disabled = !state.currentId;
|
||||
renderList();
|
||||
setStatus('已加载: ' + (data.title || data.id));
|
||||
}
|
||||
|
||||
async function saveDoc() {
|
||||
const payload = {
|
||||
id: state.currentId,
|
||||
title: $('#title-input').value.trim(),
|
||||
content: weEditor ? weEditor.getHtml() : $('#we-editor').innerHTML,
|
||||
};
|
||||
const data = await fetchJSON('api.php?action=save', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
state.currentId = data.id;
|
||||
$('#btn-save').disabled = true;
|
||||
$('#btn-delete').disabled = !state.currentId;
|
||||
await refreshList();
|
||||
renderList();
|
||||
setStatus('已保存: ' + (payload.title || data.id));
|
||||
}
|
||||
|
||||
async function deleteDoc() {
|
||||
if (!state.currentId) return;
|
||||
if (!confirm('确定删除当前文档吗?')) return;
|
||||
const data = await fetchJSON('api.php?action=delete', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ id: state.currentId }),
|
||||
});
|
||||
state.currentId = null;
|
||||
$('#title-input').value = '';
|
||||
$('#editor').innerHTML = '';
|
||||
$('#btn-save').disabled = true;
|
||||
$('#btn-delete').disabled = true;
|
||||
await refreshList();
|
||||
renderList();
|
||||
setStatus('文档已删除');
|
||||
}
|
||||
|
||||
function newDoc() {
|
||||
state.currentId = null;
|
||||
$('#title-input').value = '';
|
||||
if (weEditor) weEditor.setHtml('');
|
||||
$('#btn-save').disabled = false;
|
||||
$('#btn-delete').disabled = true;
|
||||
renderList();
|
||||
setStatus('新建文档');
|
||||
}
|
||||
|
||||
function markDirty() {
|
||||
$('#btn-save').disabled = false;
|
||||
}
|
||||
|
||||
// 由 wangEditor 处理图片上传
|
||||
|
||||
function setupEvents() {
|
||||
$('#btn-save').onclick = saveDoc;
|
||||
$('#btn-delete').onclick = deleteDoc;
|
||||
$('#btn-new').onclick = newDoc;
|
||||
$('#title-input').oninput = markDirty;
|
||||
const shareBtn = $('#btn-share');
|
||||
if (shareBtn) shareBtn.onclick = shareCurrent;
|
||||
const revokeBtn = $('#btn-share-revoke');
|
||||
if (revokeBtn) revokeBtn.onclick = revokeShares;
|
||||
}
|
||||
|
||||
async function init() {
|
||||
setupEvents();
|
||||
// 初始化 wangEditor
|
||||
const { createEditor, createToolbar } = window.wangEditor || {};
|
||||
if (createEditor && createToolbar) {
|
||||
const editorConfig = { placeholder: '在此编辑内容…' };
|
||||
editorConfig.onChange = () => markDirty();
|
||||
// 配置图片上传到后端
|
||||
editorConfig.MENU_CONF = editorConfig.MENU_CONF || {};
|
||||
editorConfig.MENU_CONF['uploadImage'] = {
|
||||
server: 'upload.php',
|
||||
fieldName: 'image',
|
||||
maxFileSize: 10 * 1024 * 1024,
|
||||
timeout: 20 * 1000,
|
||||
customInsert(res, insertFn) {
|
||||
if (res && res.url) insertFn(res.url);
|
||||
},
|
||||
};
|
||||
|
||||
weEditor = createEditor({ selector: '#we-editor', config: editorConfig, mode: 'default' });
|
||||
createToolbar({ editor: weEditor, selector: '#we-toolbar', mode: 'default' });
|
||||
}
|
||||
await refreshList();
|
||||
if (state.list.length) {
|
||||
loadDoc(state.list[0].id).catch(console.error);
|
||||
} else {
|
||||
newDoc();
|
||||
}
|
||||
}
|
||||
|
||||
async function shareCurrent() {
|
||||
if (!state.currentId) {
|
||||
alert('请先保存文档,再进行分享');
|
||||
return;
|
||||
}
|
||||
openShareModal();
|
||||
}
|
||||
|
||||
init().catch((e) => setStatus('初始化失败: ' + e.message));
|
||||
|
||||
function openShareModal() {
|
||||
const overlay = $('#share-modal');
|
||||
const linkBox = overlay.querySelector('.link-box');
|
||||
const linkInput = $('#share-link');
|
||||
const copyBtn = $('#btn-copy-link');
|
||||
overlay.style.display = 'flex';
|
||||
overlay.setAttribute('aria-hidden', 'false');
|
||||
linkBox.style.display = 'none';
|
||||
const close = () => { overlay.style.display = 'none'; overlay.setAttribute('aria-hidden', 'true'); };
|
||||
$('#btn-close-share').onclick = close;
|
||||
overlay.querySelectorAll('.options .btn').forEach((btn) => {
|
||||
btn.onclick = async () => {
|
||||
const mode = btn.getAttribute('data-mode');
|
||||
try {
|
||||
const data = await fetchJSON('api.php?action=share_create&id=' + encodeURIComponent(state.currentId) + '&mode=' + encodeURIComponent(mode));
|
||||
const url = (data && data.url) ? (location.origin + data.url) : '';
|
||||
if (!url) throw new Error('未获取到分享链接');
|
||||
linkInput.value = url;
|
||||
linkBox.style.display = 'flex';
|
||||
copyBtn.onclick = async () => {
|
||||
try { await navigator.clipboard.writeText(url); showToast('已复制分享链接:', url); } catch (e) { prompt('复制分享链接', url); }
|
||||
};
|
||||
} catch (e) {
|
||||
alert('生成分享链接失败:' + e.message);
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function showToast(text, link) {
|
||||
const el = $('#share-toast');
|
||||
el.innerHTML = link ? (text + '<a href="' + link + '" target="_blank">打开</a>') : text;
|
||||
el.style.display = 'block';
|
||||
setTimeout(() => { el.style.display = 'none'; }, 3000);
|
||||
}
|
||||
|
||||
async function revokeShares() {
|
||||
if (!state.currentId) { alert('当前没有文档'); return; }
|
||||
if (!confirm('取消该文档的所有分享链接?此操作不可恢复')) return;
|
||||
try {
|
||||
await fetchJSON('api.php?action=share_revoke_all&id=' + encodeURIComponent(state.currentId));
|
||||
setStatus('已取消所有分享');
|
||||
showToast('已取消所有分享');
|
||||
} catch (e) {
|
||||
alert('取消分享失败:' + e.message);
|
||||
}
|
||||
}
|
||||
153
assets/style.css
Normal file
153
assets/style.css
Normal file
@@ -0,0 +1,153 @@
|
||||
* { box-sizing: border-box; }
|
||||
html, body { height: 100%; }
|
||||
:root {
|
||||
--bg: #0b0e12;
|
||||
--panel: #0f1318;
|
||||
--text: #eaeaea;
|
||||
--muted: #9fb2bf;
|
||||
--accent: #ff4d4f;
|
||||
--accent-2: #ff6b6f;
|
||||
--shadow: 0 12px 30px rgba(0,0,0,.35);
|
||||
--bg-tint: #12161d;
|
||||
--scroll-thumb: #2a2f3a;
|
||||
--scroll-track: #141821;
|
||||
--toolbar-tint: rgba(255,77,79,.08);
|
||||
--input-bg: #0f1318;
|
||||
--input-border: var(--accent);
|
||||
}
|
||||
|
||||
/* 浅色主题变量覆盖 */
|
||||
:root[data-theme="light"] {
|
||||
--bg: #f7f8fa;
|
||||
--panel: #ffffff;
|
||||
--text: #1b2430;
|
||||
--muted: #6b7785;
|
||||
--accent: #ff4d4f;
|
||||
--accent-2: #ff6b6f;
|
||||
--shadow: 0 10px 26px rgba(22,28,38,.12);
|
||||
--bg-tint: #eef2f7;
|
||||
--scroll-thumb: #cfd6e2;
|
||||
--scroll-track: #e9edf3;
|
||||
--toolbar-tint: rgba(255,77,79,.03);
|
||||
--input-bg: #ffffff;
|
||||
--input-border: #e3e8ef;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', 'Microsoft Yahei', sans-serif;
|
||||
background: radial-gradient(1200px 600px at 70% 10%, var(--bg-tint) 0%, var(--bg) 55%);
|
||||
color: var(--text);
|
||||
overflow: hidden; /* 防止整页滚动 */
|
||||
}
|
||||
|
||||
.app {
|
||||
display: grid;
|
||||
grid-template-columns: 300px 1fr;
|
||||
height: 100vh;
|
||||
backdrop-filter: saturate(1.2);
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
border-right: 2px solid var(--accent);
|
||||
padding: 16px;
|
||||
background: linear-gradient(180deg, rgba(255,77,79,.06), transparent 40%);
|
||||
overflow: auto; /* 侧栏独立滚动 */
|
||||
}
|
||||
.sidebar-header { display:flex; align-items:center; justify-content:space-between; gap:12px; }
|
||||
.sidebar h1 { margin:0; font-size:18px; letter-spacing:.5px; }
|
||||
.actions { display:flex; gap:8px; }
|
||||
|
||||
.btn {
|
||||
background: #1a1f27;
|
||||
border: 1px solid var(--accent);
|
||||
color: #ffd6d6;
|
||||
padding: 8px 12px;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
transition: all .18s ease;
|
||||
}
|
||||
.btn:hover { transform: translateY(-1px); box-shadow: 0 6px 16px rgba(255,77,79,.25); }
|
||||
.btn.primary { background: var(--accent); color: var(--bg); border-color: var(--accent); }
|
||||
.btn.primary:hover { background: var(--accent-2); }
|
||||
.btn.danger { background:#2a0f12; color:#ff9aa0; border-color:var(--accent); }
|
||||
.btn[disabled] { opacity:.6; cursor:not-allowed; box-shadow:none; transform:none; }
|
||||
|
||||
/* 浅色主题按钮更柔和 */
|
||||
:root[data-theme="light"] .btn { background:#f6f8fb; color:#3b4450; border-color:#e3e8ef; }
|
||||
:root[data-theme="light"] .btn:hover { box-shadow: 0 6px 16px rgba(22,28,38,.12); }
|
||||
:root[data-theme="light"] .btn.danger { background:#ffe8ea; color:#d33; border-color:#ffc2c5; }
|
||||
|
||||
.doc-list { list-style:none; padding:0; margin:12px 0 0; display:flex; flex-direction:column; gap:8px; }
|
||||
.doc-item {
|
||||
position: relative;
|
||||
padding: 8px 10px 8px 32px;
|
||||
border: 1.5px solid var(--accent);
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
background: var(--panel);
|
||||
transition: all .2s ease;
|
||||
font-size: 14px;
|
||||
min-height: 40px;
|
||||
display: flex; align-items: center;
|
||||
}
|
||||
.doc-item::before {
|
||||
content: '\1F4C2'; /* folder emoji */
|
||||
position: absolute; left: 10px; top: 8px; opacity:.8;
|
||||
}
|
||||
.doc-item:hover { filter: brightness(1.05); transform: translateY(-1px); }
|
||||
.doc-item.active { outline: 2px solid rgba(255,77,79,.25); background: var(--panel); box-shadow: 0 0 0 3px rgba(255,77,79,.06) inset; }
|
||||
|
||||
.editor { display:flex; flex-direction:column; height: 100vh; }
|
||||
.toolbar {
|
||||
position: sticky; top: 0; z-index: 5;
|
||||
display:flex; gap:12px; align-items:center; padding:12px;
|
||||
border-bottom:2px solid var(--accent);
|
||||
background: linear-gradient(180deg, var(--toolbar-tint), transparent 90%), var(--panel);
|
||||
box-shadow: var(--shadow);
|
||||
}
|
||||
.title-input {
|
||||
flex:1; padding:10px 12px; background: var(--input-bg); color:var(--text);
|
||||
border:1px solid var(--input-border); border-radius:8px; outline:none; transition: box-shadow .15s;
|
||||
}
|
||||
.title-input:focus { box-shadow: 0 0 0 3px rgba(255,77,79,.3); }
|
||||
|
||||
.editor-area {
|
||||
flex:1; padding:18px 24px; overflow:auto; background: var(--panel);
|
||||
line-height: 1.7; font-size: 15px;
|
||||
}
|
||||
.editor-area:empty::before { content:'在此编辑内容…'; color: var(--muted); }
|
||||
.editor-area img { max-width:100%; height:auto; border-radius: 8px; box-shadow: 0 4px 22px rgba(0,0,0,.35); }
|
||||
.editor-area p { margin: .7em 0; }
|
||||
.editor-area h1, .editor-area h2, .editor-area h3 { margin: 1em 0 .6em; color:#2b2f36; }
|
||||
.editor-area a { color:#ff9aa0; }
|
||||
|
||||
.status { border-top:2px solid var(--accent); padding:8px 12px; font-size:12px; color:var(--muted); background: var(--panel); }
|
||||
|
||||
/* 精致滚动条 */
|
||||
* { scrollbar-width: thin; }
|
||||
::-webkit-scrollbar { width: 10px; height: 10px; }
|
||||
::-webkit-scrollbar-thumb { background: var(--scroll-thumb); border-radius: 6px; border:2px solid var(--scroll-track); }
|
||||
::-webkit-scrollbar-thumb:hover { filter: brightness(1.1); }
|
||||
::-webkit-scrollbar-track { background: var(--scroll-track); }
|
||||
|
||||
/* 响应式 */
|
||||
@media (max-width: 960px) {
|
||||
.app { grid-template-columns: 240px 1fr; }
|
||||
}
|
||||
@media (max-width: 720px) {
|
||||
.app { grid-template-columns: 1fr; }
|
||||
.sidebar { border-right: none; border-bottom: 2px solid var(--accent); }
|
||||
}
|
||||
/* 分享弹窗与通知条 */
|
||||
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: none; align-items: center; justify-content: center; z-index: 999; }
|
||||
.modal { background: var(--panel); color: var(--text); border: 1px solid var(--input-border); border-radius: 12px; box-shadow: var(--shadow); width: 520px; max-width: 92vw; padding: 16px; }
|
||||
.modal h3 { margin: 0 0 10px; font-size: 18px; }
|
||||
.modal .options { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
|
||||
.modal .options .btn { padding: 8px 12px; }
|
||||
.modal .link-box { display: flex; gap: 8px; align-items: center; margin-top: 10px; }
|
||||
.modal .link-box input { flex: 1; padding: 8px 10px; background: var(--input-bg); border: 1px solid var(--input-border); border-radius: 8px; color: var(--text); }
|
||||
.modal .footer { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
|
||||
|
||||
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: 16px; background: var(--panel); color: var(--text); border: 1px solid var(--input-border); border-radius: 10px; box-shadow: var(--shadow); padding: 10px 14px; z-index: 998; display: none; }
|
||||
.toast a { color: var(--accent); text-decoration: none; }
|
||||
Reference in New Issue
Block a user