edit_article.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. <div id="top-container" style="padding-left: 6px;">
  95. <p style="text-align:right;padding: 2px;">
  96. <textarea hidden="hidden" id="html_text">{{ article.html_text }}</textarea>
  97. <a class="ui button secondary mini" href="/management/article"><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" value="{{ article.title }}" placeholder="标题">
  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 value="{{ article.title }}" type="text" id="title" name="title"
  125. placeholder="标题">
  126. </div>
  127. </div>
  128. <div class="field">
  129. <label>摘要</label>
  130. <textarea type="text" name="intro"
  131. placeholder="摘要">{{ article.intro }}</textarea>
  132. </div>
  133. <div class="field">
  134. <div class="fields">
  135. <div class="field">
  136. <label>栏目</label>
  137. <select name="category" class="ui dropdown">
  138. {% for item in category %}
  139. {% if item.id == article.category %}
  140. <option selected value="{{ item.id }}">{{ item.name }}</option>
  141. {% else %}
  142. <option value="{{ item.id }}">{{ item.name }}</option>
  143. {% endif %}
  144. {% endfor %}
  145. </select>
  146. </div>
  147. <div class="field">
  148. <label>置顶</label>
  149. <select name="is_top" class="ui dropdown">
  150. {% if 1 == article.is_top %}
  151. <option selected value="1">YES</option>
  152. <option value="0">NO</option>
  153. {% else %}
  154. <option value="1">YES</option>
  155. <option selected value="0">NO</option>
  156. {% endif %}
  157. </select>
  158. </div>
  159. <div class="field">
  160. <label>Dont Check</label>
  161. <select id="dont_check" class="ui dropdown">
  162. <option selected value="0">检查</option>
  163. <option value="1">不检查</option>
  164. </select>
  165. </div>
  166. </div>
  167. </div>
  168. <div class="field">
  169. <label>标签</label>
  170. <div class="fields">
  171. <div class="sixteen wide field">
  172. <input name="tags" id="tags" value="{{ tags }}"/>
  173. </div>
  174. </div>
  175. </div>
  176. </form>
  177. </div>
  178. </div>
  179. <div class="actions">
  180. <div onclick="submit(1)" class="ui primary approve button">
  181. 发布
  182. </div>
  183. <div onclick="submit(0)" class="ui secondary approve button">
  184. 保存草稿
  185. </div>
  186. </div>
  187. </div>
  188. <!-- 显示内容 -->
  189. <script src="{% static 'js/jquery.min.js' %}"></script>
  190. <script src="{% static 'semantic.js' %}"></script>
  191. <script src="{% static 'tags-input/jquery-ui.min.js' %}"></script>
  192. <script src="{% static 'tags-input/jquery.tagsinput.min.js' %}"></script>
  193. <!-- <script src="https://cdn.jsdelivr.net/npm/@wangeditor/editor@latest/dist/index.min.js"></script> -->
  194. <script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
  195. <script>
  196. $(document).on("keydown", "form", function (event) {
  197. return event.key != "Enter";
  198. });
  199. const E = window.wangEditor
  200. // 切换语言
  201. const LANG = location.href.indexOf('lang=en') > 0 ? 'en' : 'zh-CN'
  202. E.i18nChangeLanguage(LANG)
  203. const imageToHtmlConf = {
  204. type: 'image',
  205. elemToHtml: function imageToHtml(elemNode) {
  206. const {src, alt, href = '', style = {}, width, height} = elemNode || {}
  207. if (alt === undefined || alt === null || alt === '') {
  208. return `<img src="${src}" alt="${alt}">`
  209. } else {
  210. return `<img alt="${alt}" src="${src}"/>`
  211. }
  212. }
  213. };
  214. const editorConfig = {
  215. placeholder: 'Type here...',
  216. scroll: false, // 禁止编辑器滚动
  217. MENU_CONF: {
  218. fontFamily: {
  219. fontFamilyList: [
  220. {
  221. name: '宋体',
  222. value: 'SimSun'
  223. }, {
  224. name: '黑体',
  225. value: 'SimHei'
  226. }, {
  227. name: '微软雅黑',
  228. value: 'Microsoft Yahei'
  229. }, {
  230. name: '微软正黑体',
  231. value: 'Microsoft JhengHei'
  232. }, {
  233. name: '楷体',
  234. value: 'cursive'
  235. }, {
  236. name: '华文仿宋',
  237. value: 'STFangsong'
  238. }, {
  239. name: '新宋体',
  240. value: 'NSimSun'
  241. }, {
  242. name: '仿宋',
  243. value: 'FangSong'
  244. }, {
  245. name: '苹方',
  246. value: 'PingFang SC'
  247. }, {
  248. name: '华文黑体',
  249. value: 'STHeiti'
  250. }, {
  251. name: '华文楷体',
  252. value: 'STKaiti'
  253. }, {
  254. name: '华文细黑',
  255. value: 'STXihei'
  256. }, {
  257. name: '翩翩体-简',
  258. value: 'Hanzipen SC'
  259. },
  260. {
  261. name: 'Noto',
  262. value: "'Noto Serif SC', serif,'Helvetica Neue',Arial,Helvetica,sans-serif"
  263. },
  264. 'Arial',
  265. 'Tahoma',
  266. 'Verdana'
  267. ]
  268. },
  269. uploadImage: {
  270. server: '/management/files/upload',
  271. // form-data fieldName ,默认值 'wangeditor-uploaded-image'
  272. fieldName: 'file',
  273. // 单个文件的最大体积限制,默认为 2M
  274. maxFileSize: 10 * 1024 * 1024, // 1M
  275. // 最多可上传几个文件,默认为 100
  276. maxNumberOfFiles: 10,
  277. // 选择文件时的类型限制,默认为 ['image/*'] 。如不想限制,则设置为 []
  278. allowedFileTypes: ['image/*'],
  279. // 自定义上传参数,例如传递验证的 token 等。参数会被添加到 formData 中,一起上传到服务端。
  280. meta: {
  281. csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val()
  282. },
  283. // 将 meta 拼接到 url 参数中,默认 false
  284. metaWithUrl: false,
  285. // 自定义增加 http header
  286. // 跨域是否传递 cookie ,默认为 false
  287. withCredentials: true,
  288. // 超时时间,默认为 10 秒
  289. timeout: 5 * 1000, // 5 秒
  290. }
  291. },
  292. onChange(editor) {
  293. const html = editor.getHtml()
  294. let title = $('#page_title').val()
  295. if (title === '' || title === undefined) {
  296. title = '未命名标题'
  297. }
  298. if (html.length > 50) {
  299. localStorage.setItem('article_buffer', JSON.stringify({
  300. 'title': title,
  301. 'html': html
  302. }))
  303. $('#title').val($('#page_title').val())
  304. }
  305. }
  306. }
  307. E.Boot.registerElemToHtml(imageToHtmlConf);
  308. // 先创建 editor
  309. const editor = E.createEditor({
  310. selector: '#editor-text-area',
  311. content: [],
  312. config: editorConfig
  313. })
  314. // 创建 toolbar
  315. const toolbar = E.createToolbar({
  316. editor,
  317. selector: '#editor-toolbar',
  318. config: {
  319. excludeKeys: 'fullScreen',
  320. }
  321. })
  322. setTimeout(function () {
  323. editor.dangerouslyInsertHtml($('#html_text').val())
  324. }, 200)
  325. function restore() {
  326. if (localStorage.getItem('article_buffer') !== null) {
  327. let item = JSON.parse(localStorage.getItem('article_buffer'))
  328. $('#page_title').val(item.title)
  329. editor.dangerouslyInsertHtml(item.html)
  330. }
  331. }
  332. </script>
  333. <script>
  334. function submit(flag) {
  335. if ($('#dont_check').val() == 1) {
  336. let formData = new FormData()
  337. var data = $('#article_form').serializeArray();
  338. data.forEach((item, i) => {
  339. if (item.value === '') {
  340. isPass = false
  341. }
  342. })
  343. data.forEach((item, i) => {
  344. formData.append(item.name, item.value)
  345. })
  346. formData.append('html', editor.getHtml())
  347. formData.append('markdown', editor.getText())
  348. formData.append('type', 2)
  349. formData.append('tags', $('#tags').val())
  350. formData.append('status', flag)
  351. formData.append('pk', {{article.id}})
  352. $.ajax('/management/article/edit_article', {
  353. method: 'post',
  354. data: formData,
  355. contentType: false,
  356. processData: false,
  357. success: function (res) {
  358. window.location.href = '/management/article'
  359. },
  360. error: function (err) {
  361. }
  362. })
  363. return
  364. }
  365. $(".ui.form").form({
  366. fields: {
  367. title: {
  368. identifier: "title",
  369. rules: [
  370. {
  371. type: "empty",
  372. prompt: "请输入标题"
  373. }
  374. ]
  375. },
  376. intro: {
  377. identifier: "intro",
  378. rules: [
  379. {
  380. type: "empty",
  381. prompt: "请输入摘要"
  382. }
  383. ]
  384. }
  385. },
  386. inline: true,
  387. on: "blur"
  388. });
  389. $('#title').val($('#page_title').val())
  390. if (flag === -1) {
  391. $('.ui.longer.modal')
  392. .modal('show')
  393. } else {
  394. let isPass = true
  395. var data = $('#article_form').serializeArray();
  396. data.forEach((item, i) => {
  397. if (item.value === '') {
  398. isPass = false
  399. }
  400. })
  401. if (isPass) {
  402. let formData = new FormData()
  403. data.forEach((item, i) => {
  404. formData.append(item.name, item.value)
  405. })
  406. formData.append('html', editor.getHtml())
  407. formData.append('markdown', editor.getText())
  408. formData.append('type', 1)
  409. formData.append('tags', $('#tags').val())
  410. formData.append('status', flag)
  411. formData.append('pk', {{article.id}})
  412. $.ajax('/management/article/edit_article', {
  413. method: 'post',
  414. data: formData,
  415. contentType: false,
  416. processData: false,
  417. success: function (res) {
  418. window.location.href = '/management/article'
  419. },
  420. error: function (err) {
  421. }
  422. })
  423. } else {
  424. alert('请检查表单')
  425. }
  426. }
  427. }
  428. $('#tags').tagsInput({
  429. 'autocomplete_url': '/management/tags/search',
  430. 'autocomplete': {selectFirst: true, width: '100px', autoFill: true},
  431. 'height': '100px',
  432. 'width': '100%',
  433. 'interactive': true,
  434. 'defaultText': '添加标签',
  435. 'delimiter': [','], // Or a string with a single delimiter. Ex: ';'
  436. 'removeWithBackspace': true,
  437. 'minChars': 0,
  438. 'maxChars': 0, // if not provided there is no limit
  439. 'placeholderColor': '#666666',
  440. });
  441. $(".ui.toggle.button").click(function () {
  442. $(".mobile.only.grid .ui.vertical.menu").toggle(100);
  443. });
  444. $(".ui.dropdown").dropdown();
  445. function generateIntro() {
  446. let text = editor.getText()
  447. if (text.length > 20) {
  448. $("textarea[name='intro']").val(text.substring(0, 20) + '...')
  449. } else {
  450. $("textarea[name='intro']").val(text + '...')
  451. }
  452. }
  453. </script>
  454. </body>
  455. </html>