62 lines
1.9 KiB
HTML
62 lines
1.9 KiB
HTML
{% extends "base.html" %}
|
||
{% block title %}{% if edit_id %}编辑记事本{% else %}新建记事本{% endif %}{% endblock %}
|
||
{% block content %}
|
||
<div class="page-title">
|
||
<h1>{% if edit_id %}编辑记事本{% else %}新建记事本{% endif %}</h1>
|
||
</div>
|
||
|
||
<div class="card">
|
||
<div class="card-body">
|
||
<form id="note-form">
|
||
<div class="form-group">
|
||
<label>标题</label>
|
||
<input type="text" name="title" class="form-control" id="note-title" required>
|
||
</div>
|
||
<div class="form-group">
|
||
<label>内容(支持 Markdown)</label>
|
||
<textarea name="content" class="form-control" id="note-content" style="min-height:300px;"></textarea>
|
||
</div>
|
||
<div style="display:flex;gap:8px;">
|
||
<button type="submit" class="btn btn-primary">保存</button>
|
||
<a href="/notes/" class="btn">取消</a>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
{% if edit_id %}
|
||
(async function() {
|
||
var id = {{ edit_id }};
|
||
var res = await fetch('/api/notes/' + id + '/');
|
||
var data = await res.json();
|
||
if (!data.success) { window.location.href = '/notes/'; return; }
|
||
document.getElementById('note-title').value = data.note.title;
|
||
document.getElementById('note-content').value = data.note.content;
|
||
})();
|
||
{% endif %}
|
||
|
||
document.getElementById('note-form').addEventListener('submit', async function(e) {
|
||
e.preventDefault();
|
||
var fd = new FormData(this);
|
||
fd.append('csrf_token', CSRF_TOKEN);
|
||
{% if edit_id %}
|
||
var url = '/api/notes/{{ edit_id }}/edit/';
|
||
{% else %}
|
||
var url = '/api/notes/';
|
||
{% endif %}
|
||
var r = await fetch(url, { method: 'POST', body: fd });
|
||
var data = await r.json();
|
||
if (data.success) {
|
||
{% if edit_id %}
|
||
window.location.href = '/notes/{{ edit_id }}/';
|
||
{% else %}
|
||
window.location.href = '/notes/' + data.id + '/';
|
||
{% endif %}
|
||
} else {
|
||
alert(data.error || '操作失败');
|
||
}
|
||
});
|
||
</script>
|
||
{% endblock %}
|