-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.html
More file actions
182 lines (160 loc) · 6.03 KB
/
Copy pathpost.html
File metadata and controls
182 lines (160 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>添加文章</title>
<link rel="stylesheet" href="./lib/layui/css/layui.css">
<link rel="stylesheet" href="./node_modules/simplemde/dist/simplemde.min.css">
<link rel="stylesheet" href="./node_modules//github-markdown-css/github-markdown.css">
<link rel="stylesheet" href="./lib/highlight/styles/atom-one-light.css">
</head>
<body style="padding:10px 50px">
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
<legend>添加文章</legend>
</fieldset>
<form class="layui-form layui-form-pane" action="javascript:;">
<div class="layui-form-item">
<label class="layui-form-label">图片</label>
<div class="layui-input-block">
<img id="uploadImg" src="./images/upload.png" style="width:150px;height:100px">
</div>
</div>
<div class="layui-form-item" pane="">
<label class="layui-form-label">标签</label>
<div class="layui-input-block" id="tags"></div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">标题</label>
<div class="layui-input-block">
<input id="title" type="text" name="title" required lay-verify="required" class="layui-input">
</div>
</div>
<div class="layui-form-item layui-form-text">
<label class="layui-form-label">摘要</label>
<div class="layui-input-block">
<textarea name="outline" id="outline" class="layui-textarea"></textarea>
</div>
</div>
<div class="layui-form-item layui-form-text">
<label class="layui-form-label">内容</label>
<div class="layui-input-block">
<textarea name="detail" id="detail" cols="30" rows="10"></textarea>
</div>
</div>
<div class="layui-form-item">
<button class="layui-btn" lay-submit="" lay-filter="sure">保存</button>
</div>
</form>
<script src="./node_modules/jquery/dist/jquery.min.js"></script>
<script src="./node_modules/simplemde/dist/simplemde.min.js"></script>
<script src="./lib/layui/layui.js"></script>
<script src="./lib/highlight/highlight.pack.js"></script>
<script type="module">
import util from './utils/util.js'
layui.use(['element', 'layer', 'form', 'upload'], function () {
const element = layui.element
const layer = layui.layer
const layedit = layui.layedit
const form = layui.form
const upload = layui.upload
const model = parent.model
const postsTable = parent.postsTable
const simplemed = new SimpleMDE({ element: document.getElementById('detail') })
$(".editor-preview-side").addClass("markdown-body")
$('textarea').on('input', _ => $('pre code').each(function (i, e) { hljs.highlightBlock(e) }))
let imageId = null
let tags = []
let id = util.getQueryString('id')
if (id) {
model.getPost(id).then(res => {
imageId = res.image.id
$('#uploadImg').attr('src', res.image.src)
$('#title').val(res.title)
$('#outline').val(res.outline)
simplemed.value(res.detail)
res.tags.forEach(tag => tags.push(tag.id))
model.getTags().then(res => {
res.data.forEach(value =>
$('#tags').append(`
<input type="checkbox" name="tags[]" value="${value.id}" title="${value.name}" lay-filter="tags" ${util.inArray(value.id, tags) ? "checked" : ""}>
`)
)
addTagButton()
})
})
} else {
model.getTags().then(res => {
res.data.forEach(value =>
$('#tags').append(`
<input type="checkbox" name="tags[]" value="${value.id}" title="${value.name}" lay-filter="tags">
`)
)
addTagButton()
})
}
upload.render({
elem: '#uploadImg',
url: model.baseRequestUrl + '/images',
headers: { token: model.token.get() },
before: obj => layer.load(),
done: res => {
layer.closeAll('loading')
if (res.code != 200) return layer.msg('上传失败')
imageId = res.data[0].id
$('#uploadImg').attr('src', res.data[0].src)
},
error: _ => {
layer.closeAll('loading');
layer.msg('上传失败')
}
})
function addTagButton() {
$('#tags').append(`
<input type="button" value="添加" id="addTag" class="layui-btn layui-btn-sm">
`)
form.render()
$('#addTag').on('click', function () {
layer.prompt({ title: '请输入标签名' }, (name, index) => {
model.storeTag({ name }).then(tag => {
$(this).before(`
<input type="checkbox" name="tags[]" value="${tag.id}" title="${tag.name}" lay-filter="tags" checked>
`)
tags.push(tag.id)
layer.close(index)
form.render()
})
})
})
}
form.on('checkbox(tags)', data => {
let value = parseInt(data.value)
util.inArray(value, tags) ? tags.splice($.inArray(value, tags), 1) : tags.push(value)
})
form.on('submit(sure)', res => {
let markdown = simplemed.value()
let html = simplemed.markdown(markdown)
let data = {
tags,
image_id: imageId,
title: res.field.title,
outline: res.field.outline,
detail: markdown
}
if (id) {
model.updatePost(id, data).then(_ => {
postsTable.reload({ page: { hash: 'posts', curr: parent.util.getHashPage('posts') } })
parent.layer.close(parent.layer.getFrameIndex(window.name))
})
} else {
model.storePost(data).then(_ => {
postsTable.reload({ page: { hash: 'posts', curr: 1 } })
parent.layer.close(parent.layer.getFrameIndex(window.name))
})
}
})
})
</script>
</body>
</html>