logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

export_import.vue (2603B)


  1. <template>
  2. <div class="import-export-container">
  3. <slot name="before" />
  4. <button
  5. class="btn button-default"
  6. @click="exportData"
  7. >
  8. {{ exportLabel }}
  9. </button>
  10. <button
  11. class="btn button-default"
  12. @click="importData"
  13. >
  14. {{ importLabel }}
  15. </button>
  16. <slot name="afterButtons" />
  17. <p
  18. v-if="importFailed"
  19. class="alert error"
  20. >
  21. {{ importFailedText }}
  22. </p>
  23. <slot name="afterError" />
  24. </div>
  25. </template>
  26. <script>
  27. export default {
  28. props: [
  29. 'exportObject',
  30. 'importLabel',
  31. 'exportLabel',
  32. 'importFailedText',
  33. 'validator',
  34. 'onImport',
  35. 'onImportFailure'
  36. ],
  37. data () {
  38. return {
  39. importFailed: false
  40. }
  41. },
  42. methods: {
  43. exportData () {
  44. const stringified = JSON.stringify(this.exportObject, null, 2) // Pretty-print and indent with 2 spaces
  45. // Create an invisible link with a data url and simulate a click
  46. const e = document.createElement('a')
  47. e.setAttribute('download', 'pleroma_theme.json')
  48. e.setAttribute('href', 'data:application/json;base64,' + window.btoa(stringified))
  49. e.style.display = 'none'
  50. document.body.appendChild(e)
  51. e.click()
  52. document.body.removeChild(e)
  53. },
  54. importData () {
  55. this.importFailed = false
  56. const filePicker = document.createElement('input')
  57. filePicker.setAttribute('type', 'file')
  58. filePicker.setAttribute('accept', '.json')
  59. filePicker.addEventListener('change', event => {
  60. if (event.target.files[0]) {
  61. // eslint-disable-next-line no-undef
  62. const reader = new FileReader()
  63. reader.onload = ({ target }) => {
  64. try {
  65. const parsed = JSON.parse(target.result)
  66. const valid = this.validator(parsed)
  67. if (valid) {
  68. this.onImport(parsed)
  69. } else {
  70. this.importFailed = true
  71. // this.onImportFailure(valid)
  72. }
  73. } catch (e) {
  74. // This will happen both if there is a JSON syntax error or the theme is missing components
  75. this.importFailed = true
  76. // this.onImportFailure(e)
  77. }
  78. }
  79. reader.readAsText(event.target.files[0])
  80. }
  81. })
  82. document.body.appendChild(filePicker)
  83. filePicker.click()
  84. document.body.removeChild(filePicker)
  85. }
  86. }
  87. }
  88. </script>
  89. <style lang="scss">
  90. .import-export-container {
  91. display: flex;
  92. flex-wrap: wrap;
  93. align-items: baseline;
  94. justify-content: center;
  95. }
  96. </style>