blocks
2 years ago
plugins
2 years ago
shared
2 years ago
store
2 years ago
blocks.js
5 years ago
util.js
4 years ago
util.js
115 lines
| 1 | const { useRef, useEffect } = wp.element; |
| 2 | |
| 3 | export function usePrevious(value) { |
| 4 | const ref = useRef(); |
| 5 | useEffect(() => { |
| 6 | ref.current = value; |
| 7 | }); |
| 8 | return ref.current; |
| 9 | } |
| 10 | |
| 11 | export function snackbarNotice({ status = "success", message }) { |
| 12 | wp.data.dispatch("core/notices").createNotice( |
| 13 | status, // Can be one of: success, info, warning, error. |
| 14 | message, // Text string to display. |
| 15 | { type: "snackbar" } |
| 16 | ); |
| 17 | } |
| 18 | |
| 19 | export const bytesToSize = (bytes) => { |
| 20 | var sizes = ["Bytes", "KB", "MB", "GB", "TB"]; |
| 21 | if (bytes == 0) return "0 Byte"; |
| 22 | var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); |
| 23 | return Math.round(bytes / Math.pow(1024, i), 2) + " " + sizes[i]; |
| 24 | }; |
| 25 | |
| 26 | export const toDate = (d) => { |
| 27 | d = new Date(d); |
| 28 | var hours = d.getHours(); |
| 29 | var minutes = d.getMinutes(); |
| 30 | var ampm = hours >= 12 ? "pm" : "am"; |
| 31 | hours = hours % 12; |
| 32 | hours = hours ? hours : 12; |
| 33 | minutes = minutes < 10 ? "0" + minutes : minutes; |
| 34 | |
| 35 | return ( |
| 36 | d.getDate() + |
| 37 | "-" + |
| 38 | (d.getMonth() + 1) + |
| 39 | "-" + |
| 40 | d.getFullYear() + |
| 41 | " at " + |
| 42 | hours + |
| 43 | ":" + |
| 44 | minutes + |
| 45 | ampm |
| 46 | ); |
| 47 | }; |
| 48 | |
| 49 | export function timeToSeconds(time) { |
| 50 | let pieces = time.split(":"); |
| 51 | let seconds; |
| 52 | if (pieces.length > 1) { |
| 53 | seconds = parseInt(pieces[0]) * 60; |
| 54 | } |
| 55 | return parseInt(pieces[1]) + parseInt(seconds); |
| 56 | } |
| 57 | |
| 58 | export function secondsToTime(number) { |
| 59 | let seconds = parseInt(number, 10); |
| 60 | let minutes = Math.floor(seconds / 60); |
| 61 | if (seconds < 10) { |
| 62 | seconds = "0" + seconds; |
| 63 | } |
| 64 | return minutes + ":" + seconds; |
| 65 | } |
| 66 | |
| 67 | export function sanitizeTime(time) { |
| 68 | let draft = time; |
| 69 | // remove any letters |
| 70 | draft = draft.replace(/[^\d\d:\d\d.-]/g, ""); |
| 71 | // make sure we have : |
| 72 | if (!draft.includes(":")) { |
| 73 | return `${draft}:00`; |
| 74 | } |
| 75 | |
| 76 | // must have something before :00 |
| 77 | if (draft.substr(0, draft.indexOf(":")).length === 0) { |
| 78 | draft = `0${draft}`; |
| 79 | } |
| 80 | |
| 81 | // only allow 2 characters after : |
| 82 | let index = draft.indexOf(":"); |
| 83 | draft = draft.substring(0, index + 3); |
| 84 | return draft; |
| 85 | } |
| 86 | |
| 87 | export function getProvider(src) { |
| 88 | const provider = "self-hosted"; |
| 89 | |
| 90 | if (src) { |
| 91 | const yt_rx = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/; |
| 92 | const has_match_youtube = src.match(yt_rx); |
| 93 | |
| 94 | if (has_match_youtube) { |
| 95 | return "youtube"; |
| 96 | } |
| 97 | |
| 98 | const vm_rx = /(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/; |
| 99 | const has_match_vimeo = src.match(vm_rx); |
| 100 | |
| 101 | if (has_match_vimeo) { |
| 102 | return "vimeo"; |
| 103 | } |
| 104 | |
| 105 | if (src.indexOf("https://vz-") > -1 && src.indexOf("b-cdn.net") > -1) { |
| 106 | return "bunny"; |
| 107 | } |
| 108 | |
| 109 | if (src.indexOf(".mp3") > -1) { |
| 110 | return "audio"; |
| 111 | } |
| 112 | } |
| 113 | return provider; |
| 114 | } |
| 115 |