-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathglobals.js
More file actions
165 lines (135 loc) · 4.26 KB
/
globals.js
File metadata and controls
165 lines (135 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { translate, translateChoice } from '../translations/translator';
import { nanoid as uid } from 'nanoid';
import PreviewHtml from '../components/fieldtypes/replicator/PreviewHtml';
import renderMarkdown from '@/util/markdown.js';
export function cp_url(url) {
url = Statamic.$config.get('cpUrl') + '/' + url;
return tidy_url(url);
}
export function docs_url(url) {
return tidy_url('https://statamic.dev/' + url);
}
export function resource_url(url) {
url = Statamic.$config.get('resourceUrl') + '/' + url;
return tidy_url(url);
}
export function tidy_url(url) {
return url.replace(/([^:])(\/\/+)/g, '$1/');
}
export function relative_url(url) {
return url.replace(/^(?:\/\/|[^/]+)*\//, '/');
}
export function dd(args) {
console.log(args);
}
export function data_get(obj, path, fallback = null) {
// Source: https://stackoverflow.com/a/22129960
var properties = Array.isArray(path) ? path : path.split('.');
var value = properties.reduce((prev, curr) => prev && prev[curr], obj);
return value !== undefined ? value : fallback;
}
export function data_set(obj, path, value) {
// Source: https://stackoverflow.com/a/20240290
var parts = path.split('.');
while (parts.length - 1) {
var key = parts.shift();
var shouldBeArray = parts.length ? new RegExp('^[0-9]+$').test(parts[0]) : false;
if (!(key in obj)) obj[key] = shouldBeArray ? [] : {};
obj = obj[key];
}
obj[parts[0]] = value;
}
export function clone(value) {
if (value === undefined) return undefined;
return JSON.parse(JSON.stringify(value));
}
export function tailwind_width_class(width) {
const widths = {
25: 'w-full @lg:w-1/4',
33: 'w-full @lg:w-1/3',
50: 'w-full @lg:w-1/2',
66: 'w-full @lg:w-2/3',
75: 'w-full @lg:w-3/4',
100: 'w-full',
};
return `${widths[width] || 'w-full'}`;
}
export function field_width_class(width) {
const widths = {
25: 'field-w-25',
33: 'field-w-33',
50: 'field-w-50',
66: 'field-w-66',
75: 'field-w-75',
100: 'field-w-100',
};
return `${widths[width] || 'field-w-100'}`;
}
export function markdown(value, options = {}) {
return renderMarkdown(value, options);
}
export function __(string, replacements) {
return translate(string, replacements);
}
export function __n(string, number, replacements) {
return translateChoice(string, number, replacements);
}
export function utf8btoa(stringToEncode) {
// first we convert it to utf-8
const utf8String = encodeURIComponent(stringToEncode).replace(/%([0-9A-F]{2})/g, (_, code) =>
String.fromCharCode(`0x${code}`),
);
// return base64 encoded string
return btoa(utf8String);
}
export function utf8atob(stringToDecode) {
// Decode from base64 to UTF-8 byte representation
const utf8String = atob(stringToDecode);
// Convert the UTF-8 byte representation back to a regular string
return decodeURIComponent(
utf8String
.split('')
.map((c) => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2))
.join(''),
);
}
export function uniqid() {
return uid();
}
export function truncate(string, length, ending = '...') {
if (string.length <= length) return string;
return string.substring(0, length - ending.length) + ending;
}
export function escapeHtml(string) {
return string
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"')
.replaceAll("'", ''');
}
export function replicatorPreviewHtml(html) {
return new PreviewHtml(html);
}
export function str_slug(string) {
return Statamic.$slug.create(string);
}
export function snake_case(string) {
return Statamic.$slug.separatedBy('_').create(string);
}
export function arrayAdd(items, item, index) {
return [
...items.slice(0, index),
item,
...items.slice(index, items.length)
]
}
export function arrayRemove(items, index) {
return [
...items.slice(0, index),
...items.slice(index + 1, items.length)
]
}
export function arrayMove(items, oldIndex, newIndex) {
return arrayAdd(arrayRemove(items, oldIndex), items[oldIndex], newIndex);
}