logo

pleroma-fe

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

emoji_tab.js (7933B)


  1. import { clone, assign } from 'lodash'
  2. import TabSwitcher from 'src/components/tab_switcher/tab_switcher.jsx'
  3. import StringSetting from '../helpers/string_setting.vue'
  4. import Checkbox from 'components/checkbox/checkbox.vue'
  5. import StillImage from 'components/still-image/still-image.vue'
  6. import Select from 'components/select/select.vue'
  7. import Popover from 'components/popover/popover.vue'
  8. import ConfirmModal from 'components/confirm_modal/confirm_modal.vue'
  9. import ModifiedIndicator from '../helpers/modified_indicator.vue'
  10. import EmojiEditingPopover from '../helpers/emoji_editing_popover.vue'
  11. const EmojiTab = {
  12. components: {
  13. TabSwitcher,
  14. StringSetting,
  15. Checkbox,
  16. StillImage,
  17. Select,
  18. Popover,
  19. ConfirmModal,
  20. ModifiedIndicator,
  21. EmojiEditingPopover
  22. },
  23. data () {
  24. return {
  25. knownLocalPacks: { },
  26. knownRemotePacks: { },
  27. editedMetadata: { },
  28. packName: '',
  29. newPackName: '',
  30. deleteModalVisible: false,
  31. remotePackInstance: '',
  32. remotePackDownloadAs: ''
  33. }
  34. },
  35. provide () {
  36. return { emojiAddr: this.emojiAddr }
  37. },
  38. computed: {
  39. pack () {
  40. return this.packName !== '' ? this.knownPacks[this.packName] : undefined
  41. },
  42. packMeta () {
  43. if (this.editedMetadata[this.packName] === undefined) {
  44. this.editedMetadata[this.packName] = clone(this.pack.pack)
  45. }
  46. return this.editedMetadata[this.packName]
  47. },
  48. knownPacks () {
  49. // Copy the object itself but not the children, so they are still passed by reference and modified
  50. const result = clone(this.knownLocalPacks)
  51. for (const instName in this.knownRemotePacks) {
  52. for (const instPack in this.knownRemotePacks[instName]) {
  53. result[`${instPack}@${instName}`] = this.knownRemotePacks[instName][instPack]
  54. }
  55. }
  56. return result
  57. },
  58. downloadWillReplaceLocal () {
  59. return (this.remotePackDownloadAs.trim() === '' && this.pack.remote && this.pack.remote.baseName in this.knownLocalPacks) ||
  60. (this.remotePackDownloadAs in this.knownLocalPacks)
  61. }
  62. },
  63. methods: {
  64. reloadEmoji () {
  65. this.$store.state.api.backendInteractor.reloadEmoji()
  66. },
  67. importFromFS () {
  68. this.$store.state.api.backendInteractor.importEmojiFromFS()
  69. },
  70. emojiAddr (name) {
  71. if (this.pack.remote !== undefined) {
  72. // Remote pack
  73. return `${this.pack.remote.instance}/emoji/${encodeURIComponent(this.pack.remote.baseName)}/${name}`
  74. } else {
  75. return `${this.$store.state.instance.server}/emoji/${encodeURIComponent(this.packName)}/${name}`
  76. }
  77. },
  78. createEmojiPack () {
  79. this.$store.state.api.backendInteractor.createEmojiPack(
  80. { name: this.newPackName }
  81. ).then(resp => resp.json()).then(resp => {
  82. if (resp === 'ok') {
  83. return this.refreshPackList()
  84. } else {
  85. this.displayError(resp.error)
  86. return Promise.reject(resp)
  87. }
  88. }).then(done => {
  89. this.$refs.createPackPopover.hidePopover()
  90. this.packName = this.newPackName
  91. this.newPackName = ''
  92. })
  93. },
  94. deleteEmojiPack () {
  95. this.$store.state.api.backendInteractor.deleteEmojiPack(
  96. { name: this.packName }
  97. ).then(resp => resp.json()).then(resp => {
  98. if (resp === 'ok') {
  99. return this.refreshPackList()
  100. } else {
  101. this.displayError(resp.error)
  102. return Promise.reject(resp)
  103. }
  104. }).then(done => {
  105. delete this.editedMetadata[this.packName]
  106. this.deleteModalVisible = false
  107. this.packName = ''
  108. })
  109. },
  110. metaEdited (prop) {
  111. if (!this.pack) return
  112. const def = this.pack.pack[prop] || ''
  113. const edited = this.packMeta[prop] || ''
  114. return edited !== def
  115. },
  116. savePackMetadata () {
  117. this.$store.state.api.backendInteractor.saveEmojiPackMetadata({ name: this.packName, newData: this.packMeta }).then(
  118. resp => resp.json()
  119. ).then(resp => {
  120. if (resp.error !== undefined) {
  121. this.displayError(resp.error)
  122. return
  123. }
  124. // Update actual pack data
  125. this.pack.pack = resp
  126. // Delete edited pack data, should auto-update itself
  127. delete this.editedMetadata[this.packName]
  128. })
  129. },
  130. updatePackFiles (newFiles) {
  131. this.pack.files = newFiles
  132. this.sortPackFiles(this.packName)
  133. },
  134. loadPacksPaginated (listFunction) {
  135. const pageSize = 25
  136. const allPacks = {}
  137. return listFunction({ instance: this.remotePackInstance, page: 1, pageSize: 0 })
  138. .then(data => data.json())
  139. .then(data => {
  140. if (data.error !== undefined) { return Promise.reject(data.error) }
  141. let resultingPromise = Promise.resolve({})
  142. for (let i = 0; i < Math.ceil(data.count / pageSize); i++) {
  143. resultingPromise = resultingPromise.then(() => listFunction({ instance: this.remotePackInstance, page: i, pageSize })
  144. ).then(data => data.json()).then(pageData => {
  145. if (pageData.error !== undefined) { return Promise.reject(pageData.error) }
  146. assign(allPacks, pageData.packs)
  147. })
  148. }
  149. return resultingPromise
  150. })
  151. .then(finished => allPacks)
  152. .catch(data => {
  153. this.displayError(data)
  154. })
  155. },
  156. refreshPackList () {
  157. this.loadPacksPaginated(this.$store.state.api.backendInteractor.listEmojiPacks)
  158. .then(allPacks => {
  159. this.knownLocalPacks = allPacks
  160. for (const name of Object.keys(this.knownLocalPacks)) {
  161. this.sortPackFiles(name)
  162. }
  163. })
  164. },
  165. listRemotePacks () {
  166. this.loadPacksPaginated(this.$store.state.api.backendInteractor.listRemoteEmojiPacks)
  167. .then(allPacks => {
  168. let inst = this.remotePackInstance
  169. if (!inst.startsWith('http')) { inst = 'https://' + inst }
  170. const instUrl = new URL(inst)
  171. inst = instUrl.host
  172. for (const packName in allPacks) {
  173. allPacks[packName].remote = {
  174. baseName: packName,
  175. instance: instUrl.origin
  176. }
  177. }
  178. this.knownRemotePacks[inst] = allPacks
  179. for (const pack in this.knownRemotePacks[inst]) {
  180. this.sortPackFiles(`${pack}@${inst}`)
  181. }
  182. this.$refs.remotePackPopover.hidePopover()
  183. })
  184. .catch(data => {
  185. this.displayError(data)
  186. })
  187. },
  188. downloadRemotePack () {
  189. if (this.remotePackDownloadAs.trim() === '') {
  190. this.remotePackDownloadAs = this.pack.remote.baseName
  191. }
  192. this.$store.state.api.backendInteractor.downloadRemoteEmojiPack({
  193. instance: this.pack.remote.instance, packName: this.pack.remote.baseName, as: this.remotePackDownloadAs
  194. })
  195. .then(data => data.json())
  196. .then(resp => {
  197. if (resp === 'ok') {
  198. this.$refs.dlPackPopover.hidePopover()
  199. return this.refreshPackList()
  200. } else {
  201. this.displayError(resp.error)
  202. return Promise.reject(resp)
  203. }
  204. }).then(done => {
  205. this.packName = this.remotePackDownloadAs
  206. this.remotePackDownloadAs = ''
  207. })
  208. },
  209. displayError (msg) {
  210. this.$store.dispatch('pushGlobalNotice', {
  211. messageKey: 'admin_dash.emoji.error',
  212. messageArgs: [msg],
  213. level: 'error'
  214. })
  215. },
  216. sortPackFiles (nameOfPack) {
  217. // Sort by key
  218. const sorted = Object.keys(this.knownPacks[nameOfPack].files).sort().reduce((acc, key) => {
  219. if (key.length === 0) return acc
  220. acc[key] = this.knownPacks[nameOfPack].files[key]
  221. return acc
  222. }, {})
  223. this.knownPacks[nameOfPack].files = sorted
  224. }
  225. },
  226. mounted () {
  227. this.refreshPackList()
  228. }
  229. }
  230. export default EmojiTab