newfile_new.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import React, { Component } from "react";
  2. import Router from "next/router";
  3. import { connect } from "react-redux";
  4. import ContentWrapper from "@/components/Layout/ContentWrapper";
  5. import { Row, Col, Card, CardHeader, CardBody, Button, FormGroup, Popover, PopoverHeader } from "reactstrap";
  6. // import { Editor } from 'react-draft-wysiwyg';
  7. import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
  8. import { EditorState, ContentState, convertFromHTML, convertToRaw, convertFromRaw } from 'draft-js';
  9. // import 'draft-js/dist/Draft.css';
  10. import dynamic from 'next/dynamic';
  11. const Editor = dynamic(
  12. () => import('react-draft-wysiwyg').then(mod => mod.Editor),
  13. { ssr: false })
  14. import draftToHtml from 'draftjs-to-html';
  15. import pdfMake from "pdfmake/build/pdfmake";
  16. import StateToPdfMake from "draft-js-export-pdfmake";
  17. import pdfFonts from "pdfmake/build/vfs_fonts";
  18. import { toast } from "react-toastify";
  19. import { addCatatan } from "../../../actions/catatan";
  20. import { getCsrf } from "../../../actions/security";
  21. pdfMake.vfs = pdfFonts.pdfMake.vfs;
  22. import SummerNote from "../../../components/Extras/summernote";
  23. // import ReactSummernote from 'react-summernote';
  24. // import SummerNote from "./SummerNote";
  25. // import rtf2html from "../../../components/Main/trf2html";
  26. // SummerNote.ImportCode();
  27. class NewFile extends Component {
  28. constructor(props) {
  29. super(props);
  30. this.editor = React.createRef();
  31. this.state = {
  32. // editorState: EditorState.createEmpty(),
  33. copiedd: false,
  34. judul: "",
  35. simpan: false,
  36. menu: "",
  37. };
  38. }
  39. static getInitialProps = async ({ query }) => {
  40. return { query };
  41. };
  42. componentDidMount() {
  43. const { query } = this.props;
  44. const { id } = query;
  45. }
  46. onEditorStateChange = (editorState) => {
  47. this.setState({
  48. editorState,
  49. });
  50. };
  51. handelSimpan = async (data) => {
  52. if (this.props?.user?.role.id === 2024) {
  53. Swal.fire({
  54. icon: 'error',
  55. title: 'Oops...',
  56. html: 'Maaf anda tidak memiliki akses untuk menyelesaikan<p> proses ini.</p>',
  57. confirmButtonColor: "#3e3a8e",
  58. confirmButtonText: 'Oke'
  59. })
  60. } else {
  61. const getToken = await getCsrf();
  62. const _csrf = getToken.token;
  63. const { token, query } = this.props;
  64. const { id } = query;
  65. // const isi = ("editorState", draftToHtml(convertToRaw(this.state.editorState.getCurrentContent())))
  66. const toastid = toast.loading("Please wait...");
  67. const added = await addCatatan(token, id, {
  68. "judul": this.state.judul,
  69. "isi": this.state.editorState,
  70. "menu": "Perpanjangan Sanksi"
  71. }, _csrf);
  72. if (!added) {
  73. toast.update(toastid, { render: "Error", type: "error", isLoading: false, autoClose: true, closeButton: true });
  74. } else {
  75. toast.update(toastid, { render: "Success", type: "success", isLoading: false, autoClose: true, closeButton: true });
  76. Router.push(`/app/perpanjangan-sanksi/detail?id=${id}`);
  77. // this.setState({ simpan: true })
  78. }
  79. }
  80. };
  81. handleGeneratePDF = () => {
  82. const rawContent = convertToRaw(this.state.editorState.getCurrentContent());
  83. console.log(rawContent)
  84. const stateToPdfMake = new StateToPdfMake(rawContent);
  85. console.log(stateToPdfMake.generate());
  86. pdfMake.createPdf(stateToPdfMake.generate()).download();
  87. };
  88. setHandleJudul = (e) => {
  89. this.setState({ judul: e.target.value });
  90. };
  91. CloseCopiedd = () => {
  92. setTimeout(() => {
  93. this.setState({
  94. copiedd: !this.state.copiedd
  95. });
  96. }, 1000);
  97. }
  98. Copiedd = () => this.setState({
  99. copiedd: !this.state.copiedd
  100. })
  101. convertFileToBase64(file) {
  102. return new Promise((resolve, reject) => {
  103. const reader = new FileReader()
  104. reader.readAsDataURL(file)
  105. reader.onload = () => resolve(reader.result)
  106. reader.onerror = () => reject(console.log('FileReader Error: ', error))
  107. })
  108. }
  109. onImageUpload = (f, cb, e) => {
  110. let file = f
  111. if (file.length) {
  112. file = f[0]
  113. }
  114. this.convertFileToBase64(file).then(src => {
  115. const $image = $('<img>').attr('src', src).wrap('div')
  116. this.editor.current.insertNode($image[0])
  117. })
  118. }
  119. onChange(e) {
  120. //$('span[style*="mso-ignore"]').remove()
  121. //let img = $('img[src*="file://"]').attr('loading',true);
  122. // console.log(e);
  123. }
  124. onImagePasteFromWord($imgs) {
  125. console.log("onImagePasteFromWord", $imgs);
  126. }
  127. onPaste(e) {
  128. //console.log('--------- onPaste --------', e)
  129. let items = e.originalEvent.clipboardData.items;
  130. let files = e.originalEvent.clipboardData.files;
  131. for (let i = 0; i < files.length; i++) {
  132. return e.preventDefault();
  133. }
  134. //console.log('---------- items -------------', items)
  135. //console.log('---------- files -------------', files)
  136. for (let i = 0; i < items.length; i++) {
  137. //console.log('---------- item -------------', items[i])
  138. if (items[i].type.indexOf("rtf") > -1) {
  139. items[i].getAsString(function (rtf) {
  140. // const doc = rtf2html(rtf);
  141. //const meta = doc.metadata();
  142. //console.log(doc)
  143. // doc
  144. // .render()
  145. // .then(function (htmlElements) {
  146. // var imgs = [];
  147. // //console.log('meta', meta);
  148. // //console.log('htmlElements', htmlElements);
  149. // htmlElements.forEach($html => {
  150. // $html.find('img[src*="data:image"]').each((i, el) => {
  151. // imgs.push(el);
  152. // });
  153. // //$('#test').append($html)
  154. // });
  155. // //console.log(imgs)
  156. // setTimeout(() => {
  157. // //console.log(imgs)
  158. // $("img[loading]").each((i, el) => {
  159. // if (imgs[i]) el.src = imgs[i].src;
  160. // });
  161. // }, 0);
  162. // })
  163. // .catch(error => console.error(error));
  164. });
  165. }
  166. }
  167. }
  168. render() {
  169. const { editorState } = this.state;
  170. return (
  171. <ContentWrapper>
  172. <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
  173. <link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-lite.min.css" rel="stylesheet" />
  174. <script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-lite.min.js"></script>
  175. <Row>
  176. <Col lg={12}>
  177. <Card body className="card-default">
  178. <FormGroup row>
  179. <label className="col-md-1 col-form-label">Judul Catatan</label>
  180. <div className="col-md-11">
  181. <input type="text" name="article-title" placeholder="Judul Catatan" className="mb-3 form-control form-control-lg" onChange={this.setHandleJudul} />
  182. </div>
  183. </FormGroup>
  184. {/* <SummerNote
  185. id='editor'
  186. destroy={false}
  187. // value={htmldata}
  188. options={{
  189. // lang: "zh-TW",
  190. lang: "eng",
  191. height: 800,
  192. dialogsInBody: true,
  193. toolbar: [
  194. ["style", ["style", "customStyle", "copyFormatting"]],
  195. ["font", ["bold", "italic", "underline", "strikethrough", "superscript", "subscript", "clear", "customCleaner"]],
  196. ["fontname", ["fontname", "customFont"]],
  197. ["fontsize", ["fontsizeInput"]],
  198. ['color', ['forecolor', 'backcolor']],
  199. ["para", ["ul", "ol", "listStyles", "paragraph"]],
  200. ["table", ["jTable"]],
  201. ["tableRow", ["jRowHeight"]],
  202. ["tableCol", ["jColWidth"]],
  203. ["insert", ["pasteHTML", "link", "unlink", "picture", "imageMap", "video", "customSpecialChar"]],
  204. ["anchor", ["anchor", "toc", "markAnchor", "editAnchor"]],
  205. ["comment", ["editPopover", "removePopover"]],
  206. ["view", ["fullscreen", "codeview", "help"]],
  207. ],
  208. canViewClasslist: true,
  209. tableClassName: 'jtable table-bordered',
  210. customFont: {
  211. fontNames: [
  212. 'Arial', 'Arial Black', 'Comic Sans MS', 'Courier New', 'Helvetica', 'Impact', 'Tahoma', 'Times New Roman', 'Verdana',
  213. { name: '新細明體', value: '新細明體, serif' },
  214. { name: '微軟正黑體', value: '微軟正黑體, sans-serif' },
  215. { name: '標楷體', value: '標楷體, DFKai-SB, BiauKaiTC' }
  216. ]
  217. },
  218. }}
  219. onChange={this.onEditorStateChange}
  220. onImageUpload={this.onImageUpload}
  221. onImagePasteFromWord={this.onImagePasteFromWord}
  222. onPaste={this.onPaste}
  223. onInit={e => console.log(`Using jquery version ${$().jquery}`)}
  224. ref={this.editor}
  225. /> */}
  226. <SummerNote options={{
  227. height: 350,
  228. dialogsInBody: true,
  229. toolbar: [
  230. ["style", ["style", "customStyle", "copyFormatting"]],
  231. ["font", ["bold", "italic", "underline", "strikethrough", "superscript", "subscript", "clear", "customCleaner"]],
  232. ["fontname", ["fontname", "customFont"]],
  233. ["fontsize", ["fontsizeInput"]],
  234. ['color', ['forecolor', 'backcolor']],
  235. ["para", ["ul", "ol", "listStyles", "paragraph"]],
  236. ["table", ["table", "jMerge", "jBackcolor", "jBorderColor", "jAlign", "jTableInfo", "jWidthHeightReset"]],
  237. ["tableRow", ["jRowHeight"]],
  238. ["tableCol", ["jColWidth"]],
  239. ["insert", ["pasteHTML", "link", "unlink", "picture", "imageMap", "video", "customSpecialChar"]],
  240. ["anchor", ["anchor", "toc", "markAnchor", "editAnchor"]],
  241. ["comment", ["editPopover", "removePopover"]],
  242. ["view", ["fullscreen", "codeview", "help"]],
  243. ],
  244. }}/>
  245. <br />
  246. <Row>
  247. <Col>
  248. {this.state.simpan === true ?
  249. (
  250. <div>
  251. <span className="btn-radius">
  252. <Button color id="Popover1" className="ml-0 mb-2 btn-v1-outline-purple" onClick={() => {
  253. this.CloseCopiedd()
  254. }} ><span><em className="fas fa-project-diagram float-left mt-1 mr-1" />&nbsp;Link Dokumen</span>
  255. {/* <h5 className="p-0 mt-3 float-right"> <em className="fas fa-project-diagram float-left mt-1" /> &nbsp;Link Dokumen</h5> */}
  256. </Button>
  257. </span>
  258. <Popover placement="bottom" isOpen={this.state.copiedd} target="Popover1" toggle={this.Copiedd}>
  259. <PopoverHeader>Link Berhasil Disalin</PopoverHeader>
  260. </Popover>
  261. </div>
  262. ) :
  263. (
  264. <div>
  265. <span className=" text-danger">
  266. *Klik tombol &#39;Simpan&#39; untuk mendapatkan link presensi
  267. </span>
  268. </div>
  269. )
  270. }
  271. </Col>
  272. <Col>
  273. <div>
  274. <Button color className="btn-login float-right" onClick={this.handelSimpan} disabled={this.state.judul === ""}>
  275. <span className="font-color-white">
  276. Simpan Catatan
  277. </span>
  278. </Button>
  279. </div>
  280. </Col>
  281. </Row>
  282. </Card>
  283. </Col>
  284. </Row>
  285. </ContentWrapper>
  286. );
  287. }
  288. }
  289. const mapStateToProps = (state) => ({ user: state.user, token: state.token });
  290. export default connect(mapStateToProps)(NewFile);