new_article.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. {% load static %}
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>新文章</title>
  9. <link href="https://cdn.bootcdn.net/ajax/libs/normalize/8.0.1/normalize.min.css" rel="stylesheet">
  10. <!-- <link href="https://cdn.jsdelivr.net/npm/@wangeditor/editor@latest/dist/css/style.css" rel="stylesheet"> -->
  11. <link href="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css" rel="stylesheet">
  12. <link href="{% static 'tags-input/jquery.tagsinput.min.css' %}" rel="stylesheet">
  13. <link href="{% static 'tags-input/jquery-ui.min.css' %}" rel="stylesheet">
  14. <link href="{% static 'semantic.min.css' %}" type="text/css" rel="stylesheet">
  15. <style>
  16. .ui-autocomplete {
  17. position: absolute;
  18. z-index: 100000;
  19. cursor: default;
  20. padding: 0;
  21. margin-top: 2px;
  22. list-style: none;
  23. background-color: #ffffff;
  24. border: 1px solid #ccc;
  25. -webkit-border-radius: 5px;
  26. -moz-border-radius: 5px;
  27. border-radius: 5px;
  28. -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
  29. -moz-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
  30. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
  31. }
  32. .ui-autocomplete > li {
  33. padding: 3px 20px;
  34. }
  35. .ui-autocomplete > li.ui-state-focus {
  36. background-color: #DDD;
  37. }
  38. .ui-helper-hidden-accessible {
  39. display: none;
  40. }
  41. html,
  42. body {
  43. background-color: #fff;
  44. height: 100%;
  45. overflow: hidden;
  46. color: #333;
  47. }
  48. #top-container {
  49. border-bottom: 1px solid #e8e8e8;
  50. padding-left: 30px;
  51. }
  52. #editor-toolbar {
  53. width: 1350px;
  54. background-color: #FCFCFC;
  55. margin: 0 auto;
  56. }
  57. #content {
  58. height: calc(100% - 40px);
  59. background-color: rgb(245, 245, 245);
  60. overflow-y: auto;
  61. position: relative;
  62. }
  63. #editor-container {
  64. width: 850px;
  65. margin: 30px auto 150px auto;
  66. background-color: #fff;
  67. padding: 20px 50px 50px 50px;
  68. border: 1px solid #e8e8e8;
  69. box-shadow: 0 2px 10px rgb(0 0 0 / 12%);
  70. }
  71. #title-container {
  72. padding: 20px 0;
  73. border-bottom: 1px solid #e8e8e8;
  74. }
  75. #title-container input {
  76. font-size: 30px;
  77. border: 0;
  78. outline: none;
  79. width: 100%;
  80. line-height: 1;
  81. }
  82. #editor-text-area {
  83. min-height: 900px;
  84. margin-top: 20px;
  85. }
  86. #tags_tag {
  87. border: 0;
  88. padding: 6px;
  89. height: 100%;
  90. }
  91. </style>
  92. </head>
  93. <body>
  94. {% csrf_token %}
  95. <div id="top-container" style="padding-left: 6px;">
  96. <p style="text-align:right;padding: 2px;">
  97. <a class="ui button secondary mini" href="/management"><i class="angle left icon"></i>返回</a>
  98. <a class="ui button youtube mini" id="restore" onclick="restore()"><i class="trash alternate icon"></i>恢复</a>
  99. <a class="ui button primary mini" onclick="submit(-1)"><i class="send icon"></i>发布</a>
  100. </p>
  101. </div>
  102. <div style="border-bottom: 1px solid #e8e8e8;">
  103. <div id="editor-toolbar"></div>
  104. </div>
  105. <div id="content">
  106. <div id="editor-container">
  107. <div id="title-container">
  108. <input id="page_title" placeholder="Page title...">
  109. </div>
  110. <div id="editor-text-area"></div>
  111. </div>
  112. </div>
  113. <div class="ui longer modal">
  114. <div class="header">
  115. 额外信息
  116. </div>
  117. <div class="scrolling content">
  118. <div class="description">
  119. <form onsubmit="return false" id="article_form" method="post" class="ui form">
  120. {% csrf_token %}
  121. <div class="field">
  122. <label>标题</label>
  123. <div class="field">
  124. <input readonly type="text" id="title" name="title" placeholder="标题">
  125. </div>
  126. <div class="field">
  127. <label>摘要</label>
  128. <textarea type="text" name="intro"
  129. placeholder="摘要">{{ article.intro }}</textarea>
  130. </div>
  131. </div>
  132. <div class="field">
  133. <div class="fields">
  134. <div class="field">
  135. <label>栏目</label>
  136. <select name="category" class="ui dropdown">
  137. {% for item in category %}
  138. <option value="{{ item.id }}">{{ item.name }}</option>
  139. {% endfor %}
  140. </select>
  141. </div>
  142. <div class="field">
  143. <label>置顶</label>
  144. <select name="is_top" class="ui dropdown">
  145. <option value="1">YES</option>
  146. <option value="0">NO</option>
  147. </select>
  148. </div>
  149. </div>
  150. </div>
  151. <div class="field">
  152. <label>标签</label>
  153. <div class="fields">
  154. <div class="sixteen wide field">
  155. <input name="tags" id="tags" value=""/>
  156. </div>
  157. </div>
  158. </div>
  159. </form>
  160. </div>
  161. </div>
  162. <div class="actions">
  163. <div onclick="submit(1)" class="ui primary approve button">
  164. 发布
  165. </div>
  166. <div onclick="submit(0)" class="ui secondary approve button">
  167. 保存
  168. </div>
  169. </div>
  170. </div>
  171. <!-- 显示内容 -->
  172. <script src="{% static 'js/jquery.min.js' %}"></script>
  173. <script src="{% static 'semantic.js' %}"></script>
  174. <script src="{% static 'tags-input/jquery-ui.min.js' %}"></script>
  175. <script src="{% static 'tags-input/jquery.tagsinput.min.js' %}"></script>
  176. <!-- <script src="https://cdn.jsdelivr.net/npm/@wangeditor/editor@latest/dist/index.min.js"></script> -->
  177. <script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
  178. <script>
  179. const E = window.wangEditor
  180. // 切换语言
  181. const LANG = location.href.indexOf('lang=en') > 0 ? 'en' : 'zh-CN'
  182. E.i18nChangeLanguage(LANG)
  183. const imageToHtmlConf = {
  184. type: 'image',
  185. elemToHtml: function imageToHtml(elemNode) {
  186. const {src, alt, href = '', style = {}, width, height} = elemNode || {}
  187. if (alt === undefined || alt === null || alt === '') {
  188. return `<img src="${src}" alt="${alt}">`
  189. } else {
  190. return `<img alt="${alt}" src="${src}" "/>`
  191. }
  192. }
  193. };
  194. const editorConfig = {
  195. placeholder: 'Type here...',
  196. scroll: false, // 禁止编辑器滚动
  197. MENU_CONF: {
  198. uploadImage: {
  199. server: '/management/files/upload',
  200. // form-data fieldName ,默认值 'wangeditor-uploaded-image'
  201. fieldName: 'file',
  202. // 单个文件的最大体积限制,默认为 2M
  203. maxFileSize: 4 * 1024 * 1024, // 1M
  204. // 最多可上传几个文件,默认为 100
  205. maxNumberOfFiles: 10,
  206. // 选择文件时的类型限制,默认为 ['image/*'] 。如不想限制,则设置为 []
  207. allowedFileTypes: ['image/*'],
  208. // 自定义上传参数,例如传递验证的 token 等。参数会被添加到 formData 中,一起上传到服务端。
  209. meta: {
  210. csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val()
  211. },
  212. // 将 meta 拼接到 url 参数中,默认 false
  213. metaWithUrl: false,
  214. // 自定义增加 http header
  215. // 跨域是否传递 cookie ,默认为 false
  216. withCredentials: true,
  217. // 超时时间,默认为 10 秒
  218. timeout: 5 * 1000, // 5 秒
  219. }
  220. },
  221. onChange(editor) {
  222. const html = editor.getHtml()
  223. let title = $('#page_title').val()
  224. if (title === '' || title === undefined) {
  225. title = '未命名标题'
  226. }
  227. if (html.length > 50) {
  228. localStorage.setItem('article_buffer', JSON.stringify({
  229. 'title': title,
  230. 'html': html
  231. }))
  232. $('#title').val($('#page_title').val())
  233. }
  234. }
  235. }
  236. E.Boot.registerElemToHtml(imageToHtmlConf);
  237. // 先创建 editor
  238. const editor = E.createEditor({
  239. selector: '#editor-text-area',
  240. content: [],
  241. // html: '',
  242. config: editorConfig
  243. })
  244. // 创建 toolbar
  245. const toolbar = E.createToolbar({
  246. editor,
  247. selector: '#editor-toolbar',
  248. config: {
  249. excludeKeys: 'fullScreen',
  250. }
  251. })
  252. // 点击空白处 focus 编辑器
  253. document.getElementById('editor-text-area').addEventListener('click', e => {
  254. if (e.target.id === 'editor-text-area') {
  255. editor.blur()
  256. editor.focus(true) // focus 到末尾
  257. }
  258. })
  259. function restore() {
  260. if (localStorage.getItem('article_buffer') !== null) {
  261. let item = JSON.parse(localStorage.getItem('article_buffer'))
  262. $('#page_title').val(item.title)
  263. editor.dangerouslyInsertHtml(item.html)
  264. }
  265. }
  266. </script>
  267. <script>
  268. function submit(flag) {
  269. $(".ui.form").form({
  270. fields: {
  271. title: {
  272. identifier: "title",
  273. rules: [
  274. {
  275. type: "empty",
  276. prompt: "请输入标题"
  277. }
  278. ]
  279. },
  280. intro: {
  281. identifier: "intro",
  282. rules: [
  283. {
  284. type: "empty",
  285. prompt: "请输入摘要"
  286. }
  287. ]
  288. }
  289. },
  290. inline: true,
  291. on: "blur"
  292. });
  293. $('#title').val($('#page_title').val())
  294. if (flag === -1) {
  295. $('.ui.longer.modal')
  296. .modal('show')
  297. } else {
  298. let isPass = true
  299. var data = $('#article_form').serializeArray();
  300. data.forEach((item, i) => {
  301. if (item.value === '') {
  302. isPass = false
  303. }
  304. })
  305. if (isPass) {
  306. let formData = new FormData()
  307. data.forEach((item, i) => {
  308. formData.append(item.name, item.value)
  309. })
  310. formData.append('html', editor.getHtml())
  311. formData.append('markdown', editor.getText())
  312. formData.append('type', 1)
  313. formData.append('tags', $('#tags').val())
  314. formData.append('status', flag)
  315. $.ajax('/management/article/add_article', {
  316. method: 'post',
  317. data: formData,
  318. contentType: false,
  319. processData: false,
  320. success: function (res) {
  321. window.location.href = '/management/article'
  322. },
  323. error: function (err) {
  324. }
  325. })
  326. } else {
  327. alert('请检查表单')
  328. }
  329. }
  330. }
  331. $('#tags').tagsInput({
  332. 'autocomplete_url': '/management/tags/search',
  333. 'autocomplete': {selectFirst: true, width: '100px', autoFill: true},
  334. 'height': '100px',
  335. 'width': '100%',
  336. 'interactive': true,
  337. 'defaultText': '添加标签',
  338. 'delimiter': [','], // Or a string with a single delimiter. Ex: ';'
  339. 'removeWithBackspace': true,
  340. 'minChars': 0,
  341. 'maxChars': 0, // if not provided there is no limit
  342. 'placeholderColor': '#666666',
  343. });
  344. $(".ui.toggle.button").click(function () {
  345. $(".mobile.only.grid .ui.vertical.menu").toggle(100);
  346. });
  347. $(".ui.dropdown").dropdown();
  348. function generateIntro() {
  349. let text = editor.getText()
  350. if (text.length > 20) {
  351. $("input[name='intro']").val(text.substring(0, 20) + '...')
  352. } else {
  353. $("input[name='intro']").val(text + '...')
  354. }
  355. }
  356. </script>
  357. </body>
  358. </html>