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