You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.6 KiB

  1. import filesize from 'filesize.js'
  2. import _ from 'lodash'
  3. /* global siteConfig */
  4. const helpers = {
  5. /**
  6. * Convert bytes to humanized form
  7. * @param {number} rawSize Size in bytes
  8. * @returns {string} Humanized file size
  9. */
  10. filesize (rawSize) {
  11. return _.toUpper(filesize(rawSize))
  12. },
  13. /**
  14. * Convert raw path to safe path
  15. * @param {string} rawPath Raw path
  16. * @returns {string} Safe path
  17. */
  18. makeSafePath (rawPath) {
  19. let rawParts = _.split(_.trim(rawPath), '/')
  20. rawParts = _.map(rawParts, (r) => {
  21. return _.kebabCase(_.deburr(_.trim(r)))
  22. })
  23. return _.join(_.filter(rawParts, (r) => { return !_.isEmpty(r) }), '/')
  24. },
  25. resolvePath (path) {
  26. if (_.startsWith(path, '/')) { path = path.substring(1) }
  27. return `${siteConfig.path}${path}`
  28. },
  29. /**
  30. * Set Input Selection
  31. * @param {DOMElement} input The input element
  32. * @param {number} startPos The starting position
  33. * @param {nunber} endPos The ending position
  34. */
  35. setInputSelection (input, startPos, endPos) {
  36. input.focus()
  37. if (typeof input.selectionStart !== 'undefined') {
  38. input.selectionStart = startPos
  39. input.selectionEnd = endPos
  40. } else if (document.selection && document.selection.createRange) {
  41. // IE branch
  42. input.select()
  43. var range = document.selection.createRange()
  44. range.collapse(true)
  45. range.moveEnd('character', endPos)
  46. range.moveStart('character', startPos)
  47. range.select()
  48. }
  49. }
  50. }
  51. export default {
  52. install(Vue) {
  53. Vue.$helpers = helpers
  54. Object.defineProperties(Vue.prototype, {
  55. $helpers: {
  56. get() {
  57. return helpers
  58. }
  59. }
  60. })
  61. }
  62. }