integrate-a-heavy-vue-component.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package digging_deeper
  2. import (
  3. "github.com/qor5/docs/docsrc/examples/e00_basics"
  4. "github.com/qor5/docs/docsrc/generated"
  5. "github.com/qor5/docs/docsrc/utils"
  6. . "github.com/theplant/docgo"
  7. "github.com/theplant/docgo/ch"
  8. )
  9. var IntegrateAHeavyVueComponent = Doc(
  10. Markdown(`
  11. We can abstract any complicated of server side render component with [htmlgo](https://github.com/theplant/htmlgo).
  12. But a lots of components in the modern web have done many things on the client side. means there are many logic
  13. happens before the it interact with server side.
  14. Here is an example, a rich text editor. you have a toolbar of buttons that you can interact, most of them won't
  15. need to communicate with server. We are going to integrate the fantastic rich text editor [tiptap](https://tiptap.scrumpy.io/)
  16. to be used as any ~htmlgo.HTMLComponent~.
  17. **Step 1**: [Create a @vue/cli project](https://cli.vuejs.org/guide/creating-a-project.html):
  18. ~~~
  19. $ vue create tiptapjs
  20. ~~~
  21. Modify or add a separate ~vue.config.js~ config file,
  22. `),
  23. ch.Code(generated.TipTapVueConfig).Language("javascript"),
  24. Markdown(`
  25. - Enable ~runtimeCompiler~ so that vue can parse template html generate from server.
  26. - Made ~Vue~ as externals so that it won't be packed to the dist production js file,
  27. Since we will be sharing one Vue.js for in one page with other libraries.
  28. - Config svg module to inline the svg icons used by tiptap
  29. **Step 2**: Create a vue component that use tiptap
  30. Install ~tiptap~ and ~tiptap-extensions~ first
  31. ~~~
  32. $ yarn add tiptap tiptap-extensions
  33. ~~~
  34. And write the ~editor.vue~ something like this, We omitted the template at here.
  35. `),
  36. ch.Code(generated.TipTapEditorVueComponent).Language("javascript"),
  37. Markdown(`
  38. We injected the ~this.$plaid()~. that is from ~web/corejs~, Which you will need to use
  39. For every Go Plaid web applications. Here we uses one function ~fieldValue~ from it.
  40. It set the form value when the rich text editor changes. So that later when you call
  41. ~EventFunc~ it the value will be posted to the server side. Here we will post the html value.
  42. Also allow component user to set ~fieldName~, which is important when posting the value to the
  43. server.
  44. **Step 3**: At ~main.js~, Use a special hook to register the component to ~web/corejs~
  45. `),
  46. ch.Code(`import TipTapEditor from './editor.vue'
  47. (window.__goplaidVueComponentRegisters =
  48. window.__goplaidVueComponentRegisters || []).push((Vue) => {
  49. Vue.component('tiptap-editor', TipTapEditor)
  50. });`).Language("go"),
  51. Markdown(`
  52. **Step 4**: Test the component in a simple html
  53. We edited the ~index.html~ inside public to be the following:
  54. `),
  55. ch.Code(generated.TipTapDemoHTML).Language("html"),
  56. Markdown(`
  57. - For ~http://localhost:3500/app.js~ to be able to serve. you have to run ~yarn serve~ in
  58. tiptapjs directory.
  59. - ~http://localhost:3100/app.js~ is QOR5 web corejs vue project.
  60. So go to that directory and run ~yarn serve~ to start it. and then in
  61. - Run a web server inside tiptapjs directory like ~python -m SimpleHTTPServer~ and point your
  62. Browser to the index.html file, and see if your vue component can render and behave correctly.
  63. **Step 5**: Use [packr](https://github.com/gobuffalo/packr) to pack the dist folder
  64. We write a packr box inside ~tiptapjs.go~ along side the tiptapjs folder.
  65. `),
  66. ch.Code(generated.TipTapPackrSample).Language("go"),
  67. Markdown(`
  68. And write a ~build.sh~ to build the javascript to production version, and run packr to pack
  69. them into ~a_tiptap-packr.go~ file.
  70. `),
  71. ch.Code(generated.TiptapBuilderSH).Language("bash"),
  72. Markdown(`
  73. **Step 6**: Write a Go wrapper to wrap it to be a ~HTMLComponent~
  74. `),
  75. ch.Code(generated.TipTapEditorHTMLComponent).Language("go"),
  76. Markdown(`
  77. **Step 7**: Use it in your web app
  78. To use it, first we have to mount the assets into our app
  79. `),
  80. ch.Code(generated.TipTapComponentsPackSample).Language("go"),
  81. Markdown(`
  82. And reference them in our layout function.
  83. `),
  84. ch.Code(generated.TipTapLayoutSample).Language("go"),
  85. Markdown(`
  86. And we write a page func to use it like any other component:
  87. `),
  88. ch.Code(generated.HelloWorldTipTapSample).Language("go"),
  89. Markdown(`
  90. And now let's check out our fruits:
  91. `),
  92. utils.Demo("Integrate a Heavy Vue Component", e00_basics.HelloWorldTipTapPath, "e00_basics/use-tiptap-editor.go"),
  93. ).Title("Integrate a heavy Vue Component").
  94. Slug("components-guide/integrate-a-heavy-vue-component")