]> Untitled Git - lemmy.git/blob - ui/src/utils.ts
Spanish translations
[lemmy.git] / ui / src / utils.ts
1 import 'moment/locale/es';
2 import 'moment/locale/eo';
3 import 'moment/locale/de';
4 import 'moment/locale/zh-cn';
5 import 'moment/locale/fr';
6 import 'moment/locale/sv';
7 import 'moment/locale/ru';
8 import 'moment/locale/nl';
9
10 import { UserOperation, Comment, User, SortType, ListingType, SearchType } from './interfaces';
11 import * as markdown_it from 'markdown-it';
12 import * as markdownitEmoji from 'markdown-it-emoji/light';
13 import * as markdown_it_container from 'markdown-it-container';
14 import * as twemoji from 'twemoji';
15 import * as emojiShortName from 'emoji-short-name';
16
17 export const repoUrl = 'https://github.com/dessalines/lemmy';
18 export const markdownHelpUrl = 'https://commonmark.org/help/';
19
20 export const postRefetchSeconds: number = 60*1000;
21 export const fetchLimit: number = 20;
22 export const mentionDropdownFetchLimit = 6;
23
24 export function randomStr() {return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(2, 10)}
25
26 export function msgOp(msg: any): UserOperation {
27   let opStr: string = msg.op;
28   return UserOperation[opStr];
29 }
30
31 export const md = new markdown_it({
32   html: false,
33   linkify: true,
34   typographer: true
35 }).use(markdown_it_container, 'spoiler', {
36   validate: function(params: any) {
37     return params.trim().match(/^spoiler\s+(.*)$/);
38   },
39
40   render: function (tokens: any, idx: any) {
41     var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
42
43     if (tokens[idx].nesting === 1) {
44       // opening tag
45       return '<details><summary>' + md.utils.escapeHtml(m[1]) + '</summary>\n';
46
47     } else {
48       // closing tag
49       return '</details>\n';
50     }
51   }
52 }).use(markdownitEmoji, {
53   defs: objectFlip(emojiShortName)
54 });
55
56 md.renderer.rules.emoji = function(token, idx) {
57   return twemoji.parse(token[idx].content);
58 };
59
60 export function hotRank(comment: Comment): number {
61   // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
62
63   let date: Date = new Date(comment.published + 'Z'); // Add Z to convert from UTC date
64   let now: Date = new Date();
65   let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
66
67   let rank = (10000 *  Math.log10(Math.max(1, 3 + comment.score))) / Math.pow(hoursElapsed + 2, 1.8);
68
69   // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
70
71   return rank;
72 }
73
74 export function mdToHtml(text: string) {
75   return {__html: md.render(text)};
76 }
77
78 export function getUnixTime(text: string): number { 
79   return text ? new Date(text).getTime()/1000 : undefined;
80 }
81
82 export function addTypeInfo<T>(arr: Array<T>, name: string): Array<{type_: string, data: T}> {  
83   return arr.map(e => {return {type_: name, data: e}});
84 }
85
86 export function canMod(user: User, modIds: Array<number>, creator_id: number, onSelf: boolean = false): boolean {
87   // You can do moderator actions only on the mods added after you.
88   if (user) {
89     let yourIndex = modIds.findIndex(id => id == user.id);
90     if (yourIndex == -1) {
91       return false;
92     } else { 
93       // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
94       modIds = modIds.slice(0, yourIndex+(onSelf ? 0 : 1)); 
95       return !modIds.includes(creator_id);
96     }
97   } else {
98     return false;
99   }
100 }
101
102 export function isMod(modIds: Array<number>, creator_id: number): boolean {
103   return modIds.includes(creator_id);
104 }
105
106
107 var imageRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:png|jpg|jpeg|gif|png|svg))`);
108 var videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
109
110 export function isImage(url: string) {
111   return imageRegex.test(url);
112 }
113
114 export function isVideo(url: string) {
115   return videoRegex.test(url);
116 }
117
118 export function validURL(str: string) {
119   var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
120     '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
121     '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
122     '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
123     '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
124     '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
125   return !!pattern.test(str);
126 }
127
128 export function capitalizeFirstLetter(str: string): string {
129   return str.charAt(0).toUpperCase() + str.slice(1);
130 }
131
132
133 export function routeSortTypeToEnum(sort: string): SortType {
134   if (sort == 'new') {
135     return SortType.New;
136   } else if (sort == 'hot') {
137     return SortType.Hot;
138   } else if (sort == 'topday') {
139     return SortType.TopDay;
140   } else if (sort == 'topweek') {
141     return SortType.TopWeek;
142   } else if (sort == 'topmonth') {
143     return SortType.TopMonth;
144   } else if (sort == 'topall') {
145     return SortType.TopAll;
146   }
147 }
148
149 export function routeListingTypeToEnum(type: string): ListingType {
150   return ListingType[capitalizeFirstLetter(type)];
151 }
152
153 export function routeSearchTypeToEnum(type: string): SearchType {
154   return SearchType[capitalizeFirstLetter(type)];
155 }
156
157 export async function getPageTitle(url: string) {
158   let res = await fetch(`https://textance.herokuapp.com/title/${url}`);
159   let data = await res.text();
160   return data;
161 }
162
163 export function debounce(func: any, wait: number = 500, immediate: boolean = false) {
164   // 'private' variable for instance
165   // The returned function will be able to reference this due to closure.
166   // Each call to the returned function will share this common timer.
167   let timeout: number;
168
169   // Calling debounce returns a new anonymous function
170   return function() {
171     // reference the context and args for the setTimeout function
172     var context = this,
173     args = arguments;
174
175   // Should the function be called now? If immediate is true
176   //   and not already in a timeout then the answer is: Yes
177   var callNow = immediate && !timeout;
178
179   // This is the basic debounce behaviour where you can call this 
180   //   function several times, but it will only execute once 
181   //   [before or after imposing a delay]. 
182   //   Each time the returned function is called, the timer starts over.
183   clearTimeout(timeout);
184
185   // Set the new timeout
186   timeout = setTimeout(function() {
187
188     // Inside the timeout function, clear the timeout variable
189     // which will let the next execution run when in 'immediate' mode
190     timeout = null;
191
192     // Check if the function already ran with the immediate flag
193     if (!immediate) {
194       // Call the original function with apply
195       // apply lets you define the 'this' object as well as the arguments 
196       //    (both captured before setTimeout)
197       func.apply(context, args);
198     }
199   }, wait);
200
201   // Immediate mode and no wait timer? Execute the function..
202   if (callNow) func.apply(context, args);
203   }
204 }
205
206 export function getLanguage(): string {
207   return (navigator.language || navigator.userLanguage);
208 }
209
210 export function objectFlip(obj: any) {
211   const ret = {};
212   Object.keys(obj).forEach((key) => {
213     ret[obj[key]] = key;
214   });
215   return ret;
216 }
217
218 export function getMomentLanguage(): string {
219   let lang = getLanguage();
220   if (lang.startsWith('zh')) {
221     lang = 'zh-cn';
222   } else if (lang.startsWith('sv')) {
223     lang = 'sv';
224   } else if (lang.startsWith('fr')) {
225     lang = 'fr';
226   } else if (lang.startsWith('de')) {
227     lang = 'de';
228   } else if (lang.startsWith('ru')) {
229     lang = 'ru';
230   } else if (lang.startsWith('es')) {
231     lang = 'es';
232   } else if (lang.startsWith('eo')) {
233     lang = 'eo';
234   } else if (lang.startsWith('nl')) {
235     lang = 'nl';
236   } else {
237     lang = 'en';
238   }
239   return lang;
240 }
241
242 export const themes = ['litera', 'minty', 'solar', 'united', 'cyborg','darkly', 'journal', 'sketchy'];
243
244 export function setTheme(theme: string = 'darkly') {
245   for (var i=0; i < themes.length; i++) {
246
247     let styleSheet = document.getElementById(themes[i]);
248     if (themes[i] == theme) {
249       styleSheet.removeAttribute("disabled");
250     } else {
251       styleSheet.setAttribute("disabled", "disabled");
252     }      
253   }
254 }