new_article.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. fontFamily: {
  199. fontFamilyList: [
  200. {
  201. name: '宋体',
  202. value: 'SimSun'
  203. }, {
  204. name: '黑体',
  205. value: 'SimHei'
  206. }, {
  207. name: '微软雅黑',
  208. value: 'Microsoft Yahei'
  209. }, {
  210. name: '微软正黑体',
  211. value: 'Microsoft JhengHei'
  212. }, {
  213. name: '楷体',
  214. value: 'cursive'
  215. }, {
  216. name: '华文仿宋',
  217. value: 'STFangsong'
  218. }, {
  219. name: '新宋体',
  220. value: 'NSimSun'
  221. }, {
  222. name: '仿宋',
  223. value: 'FangSong'
  224. }, {
  225. name: '苹方',
  226. value: 'PingFang SC'
  227. }, {
  228. name: '华文黑体',
  229. value: 'STHeiti'
  230. }, {
  231. name: '华文楷体',
  232. value: 'STKaiti'
  233. }, {
  234. name: '华文细黑',
  235. value: 'STXihei'
  236. }, {
  237. name: '翩翩体-简',
  238. value: 'Hanzipen SC'
  239. },
  240. {
  241. name: 'Noto',
  242. value: "'Noto Serif SC', serif,'Helvetica Neue',Arial,Helvetica,sans-serif"
  243. },
  244. 'Arial',
  245. 'Tahoma',
  246. 'Verdana'
  247. ]
  248. },
  249. uploadImage: {
  250. server: '/management/files/upload',
  251. // form-data fieldName ,默认值 'wangeditor-uploaded-image'
  252. fieldName: 'file',
  253. // 单个文件的最大体积限制,默认为 2M
  254. maxFileSize: 10 * 1024 * 1024, // 1M
  255. // 最多可上传几个文件,默认为 100
  256. maxNumberOfFiles: 10,
  257. // 选择文件时的类型限制,默认为 ['image/*'] 。如不想限制,则设置为 []
  258. allowedFileTypes: ['image/*'],
  259. // 自定义上传参数,例如传递验证的 token 等。参数会被添加到 formData 中,一起上传到服务端。
  260. meta: {
  261. csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val()
  262. },
  263. // 将 meta 拼接到 url 参数中,默认 false
  264. metaWithUrl: false,
  265. // 自定义增加 http header
  266. // 跨域是否传递 cookie ,默认为 false
  267. withCredentials: true,
  268. // 超时时间,默认为 10 秒
  269. timeout: 5 * 1000, // 5 秒
  270. }
  271. },
  272. onChange(editor) {
  273. const html = editor.getHtml()
  274. let title = $('#page_title').val()
  275. if (title === '' || title === undefined) {
  276. title = '未命名标题'
  277. }
  278. if (html.length > 50) {
  279. localStorage.setItem('article_buffer', JSON.stringify({
  280. 'title': title,
  281. 'html': html
  282. }))
  283. $('#title').val($('#page_title').val())
  284. }
  285. }
  286. }
  287. E.Boot.registerElemToHtml(imageToHtmlConf);
  288. // 先创建 editor
  289. const editor = E.createEditor({
  290. selector: '#editor-text-area',
  291. content: [],
  292. // html: '',
  293. config: editorConfig
  294. })
  295. // 创建 toolbar
  296. const toolbar = E.createToolbar({
  297. editor,
  298. selector: '#editor-toolbar',
  299. config: {
  300. excludeKeys: 'fullScreen',
  301. }
  302. })
  303. // 点击空白处 focus 编辑器
  304. document.getElementById('editor-text-area').addEventListener('click', e => {
  305. if (e.target.id === 'editor-text-area') {
  306. editor.blur()
  307. editor.focus(true) // focus 到末尾
  308. }
  309. })
  310. function restore() {
  311. if (localStorage.getItem('article_buffer') !== null) {
  312. let item = JSON.parse(localStorage.getItem('article_buffer'))
  313. $('#page_title').val(item.title)
  314. editor.dangerouslyInsertHtml(item.html)
  315. }
  316. }
  317. </script>
  318. <script>
  319. function submit(flag) {
  320. $(".ui.form").form({
  321. fields: {
  322. title: {
  323. identifier: "title",
  324. rules: [
  325. {
  326. type: "empty",
  327. prompt: "请输入标题"
  328. }
  329. ]
  330. },
  331. intro: {
  332. identifier: "intro",
  333. rules: [
  334. {
  335. type: "empty",
  336. prompt: "请输入摘要"
  337. }
  338. ]
  339. }
  340. },
  341. inline: true,
  342. on: "blur"
  343. });
  344. $('#title').val($('#page_title').val())
  345. if (flag === -1) {
  346. $('.ui.longer.modal')
  347. .modal('show')
  348. } else {
  349. let isPass = true
  350. var data = $('#article_form').serializeArray();
  351. data.forEach((item, i) => {
  352. if (item.value === '') {
  353. isPass = false
  354. }
  355. })
  356. if (isPass) {
  357. let formData = new FormData()
  358. data.forEach((item, i) => {
  359. formData.append(item.name, item.value)
  360. })
  361. formData.append('html', editor.getHtml())
  362. formData.append('markdown', editor.getText())
  363. formData.append('type', 1)
  364. formData.append('tags', $('#tags').val())
  365. formData.append('status', flag)
  366. $.ajax('/management/article/add_article', {
  367. method: 'post',
  368. data: formData,
  369. contentType: false,
  370. processData: false,
  371. success: function (res) {
  372. window.location.href = '/management/article'
  373. },
  374. error: function (err) {
  375. }
  376. })
  377. } else {
  378. alert('请检查表单')
  379. }
  380. }
  381. }
  382. $('#tags').tagsInput({
  383. 'autocomplete_url': '/management/tags/search',
  384. 'autocomplete': {selectFirst: true, width: '100px', autoFill: true},
  385. 'height': '100px',
  386. 'width': '100%',
  387. 'interactive': true,
  388. 'defaultText': '添加标签',
  389. 'delimiter': [','], // Or a string with a single delimiter. Ex: ';'
  390. 'removeWithBackspace': true,
  391. 'minChars': 0,
  392. 'maxChars': 0, // if not provided there is no limit
  393. 'placeholderColor': '#666666',
  394. });
  395. $(".ui.toggle.button").click(function () {
  396. $(".mobile.only.grid .ui.vertical.menu").toggle(100);
  397. });
  398. $(".ui.dropdown").dropdown();
  399. function generateIntro() {
  400. let text = editor.getText()
  401. if (text.length > 20) {
  402. $("input[name='intro']").val(text.substring(0, 20) + '...')
  403. } else {
  404. $("input[name='intro']").val(text + '...')
  405. }
  406. }
  407. </script>
  408. </body>
  409. </html>