1 import 'moment/locale/es';
2 import 'moment/locale/el';
3 import 'moment/locale/eu';
4 import 'moment/locale/eo';
5 import 'moment/locale/de';
6 import 'moment/locale/zh-cn';
7 import 'moment/locale/fr';
8 import 'moment/locale/sv';
9 import 'moment/locale/ru';
10 import 'moment/locale/nl';
11 import 'moment/locale/it';
12 import 'moment/locale/fi';
13 import 'moment/locale/ca';
14 import 'moment/locale/fa';
15 import 'moment/locale/pl';
16 import 'moment/locale/pt-br';
17 import 'moment/locale/ja';
18 import 'moment/locale/ka';
19 import 'moment/locale/hi';
20 import 'moment/locale/gl';
21 import 'moment/locale/tr';
22 import 'moment/locale/hu';
23 import 'moment/locale/uk';
24 import 'moment/locale/sq';
25 import 'moment/locale/km';
26 import 'moment/locale/ga';
27 import 'moment/locale/sr';
28 import 'moment/locale/ko';
33 CommentNode as CommentNodeI,
41 WebSocketJsonResponse,
47 } from 'lemmy-js-client';
49 import { httpUri } from './env';
51 import { CommentSortType, DataType, IsoData } from './interfaces';
52 import { UserService, WebSocketService } from './services';
56 Tribute = require('tributejs');
58 import markdown_it from 'markdown-it';
59 import markdown_it_sub from 'markdown-it-sub';
60 import markdown_it_sup from 'markdown-it-sup';
61 import markdownitEmoji from 'markdown-it-emoji/light';
62 import markdown_it_container from 'markdown-it-container';
63 import emojiShortName from 'emoji-short-name';
64 import Toastify from 'toastify-js';
65 import tippy from 'tippy.js';
66 import moment from 'moment';
67 import { Subscription } from 'rxjs';
68 import { retryWhen, delay, take } from 'rxjs/operators';
70 export const favIconUrl = '/static/assets/favicon.svg';
71 export const favIconPngUrl = '/static/assets/apple-touch-icon.png';
73 // export const defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`;
74 export const defaultFavIcon = 'test';
75 export const repoUrl = 'https://github.com/LemmyNet';
76 export const helpGuideUrl = '/docs/about_guide.html';
77 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
78 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
79 export const archiveUrl = 'https://archive.is';
80 export const elementUrl = 'https://element.io/';
82 export const postRefetchSeconds: number = 60 * 1000;
83 export const fetchLimit: number = 20;
84 export const mentionDropdownFetchLimit = 10;
86 export const lemmyHttp = new LemmyHttp(httpUri);
88 export const languages = [
89 { code: 'ca', name: 'Català' },
90 { code: 'en', name: 'English' },
91 { code: 'el', name: 'Ελληνικά' },
92 { code: 'eu', name: 'Euskara' },
93 { code: 'eo', name: 'Esperanto' },
94 { code: 'es', name: 'Español' },
95 { code: 'de', name: 'Deutsch' },
96 { code: 'ga', name: 'Gaeilge' },
97 { code: 'gl', name: 'Galego' },
98 { code: 'hu', name: 'Magyar Nyelv' },
99 { code: 'ka', name: 'ქართული ენა' },
100 { code: 'ko', name: '한국어' },
101 { code: 'km', name: 'ភាសាខ្មែរ' },
102 { code: 'hi', name: 'मानक हिन्दी' },
103 { code: 'fa', name: 'فارسی' },
104 { code: 'ja', name: '日本語' },
105 { code: 'pl', name: 'Polski' },
106 { code: 'pt_BR', name: 'Português Brasileiro' },
107 { code: 'zh', name: '中文' },
108 { code: 'fi', name: 'Suomi' },
109 { code: 'fr', name: 'Français' },
110 { code: 'sv', name: 'Svenska' },
111 { code: 'sq', name: 'Shqip' },
112 { code: 'sr_Latn', name: 'srpski' },
113 { code: 'tr', name: 'Türkçe' },
114 { code: 'uk', name: 'Українська Mова' },
115 { code: 'ru', name: 'Русский' },
116 { code: 'nl', name: 'Nederlands' },
117 { code: 'it', name: 'Italiano' },
120 export const themes = [
136 const DEFAULT_ALPHABET =
137 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
139 function getRandomCharFromAlphabet(alphabet: string): string {
140 return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
143 export function randomStr(
144 idDesiredLength: number = 20,
145 alphabet = DEFAULT_ALPHABET
148 * Create n-long array and map it to random chars from given alphabet.
149 * Then join individual chars as string
151 return Array.from({ length: idDesiredLength })
153 return getRandomCharFromAlphabet(alphabet);
158 export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
159 let opStr: string = msg.op;
161 op: UserOperation[opStr],
166 export const md = new markdown_it({
171 .use(markdown_it_sub)
172 .use(markdown_it_sup)
173 .use(markdown_it_container, 'spoiler', {
174 validate: function (params: any) {
175 return params.trim().match(/^spoiler\s+(.*)$/);
178 render: function (tokens: any, idx: any) {
179 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
181 if (tokens[idx].nesting === 1) {
183 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
186 return '</details>\n';
190 .use(markdownitEmoji, {
191 defs: objectFlip(emojiShortName),
194 export function hotRankComment(comment: Comment): number {
195 return hotRank(comment.score, comment.published);
198 export function hotRankPost(post: Post): number {
199 return hotRank(post.score, post.newest_activity_time);
202 export function hotRank(score: number, timeStr: string): number {
203 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
204 let date: Date = new Date(timeStr + 'Z'); // Add Z to convert from UTC date
205 let now: Date = new Date();
206 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
209 (10000 * Math.log10(Math.max(1, 3 + score))) /
210 Math.pow(hoursElapsed + 2, 1.8);
212 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
217 export function mdToHtml(text: string) {
218 return { __html: md.render(text) };
221 export function getUnixTime(text: string): number {
222 return text ? new Date(text).getTime() / 1000 : undefined;
225 export function addTypeInfo<T>(
228 ): { type_: string; data: T }[] {
229 return arr.map(e => {
230 return { type_: name, data: e };
234 export function canMod(
238 onSelf: boolean = false
240 // You can do moderator actions only on the mods added after you.
242 let yourIndex = modIds.findIndex(id => id == user.id);
243 if (yourIndex == -1) {
246 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
247 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
248 return !modIds.includes(creator_id);
255 export function isMod(modIds: number[], creator_id: number): boolean {
256 return modIds.includes(creator_id);
259 const imageRegex = new RegExp(
260 /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/
262 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
264 export function isImage(url: string) {
265 return imageRegex.test(url);
268 export function isVideo(url: string) {
269 return videoRegex.test(url);
273 export function validURL(str: string) {
275 return !!new URL(str);
281 export function validEmail(email: string) {
282 let re = /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
283 return re.test(String(email).toLowerCase());
286 export function capitalizeFirstLetter(str: string): string {
287 return str.charAt(0).toUpperCase() + str.slice(1);
290 export function routeSortTypeToEnum(sort: string): SortType {
291 return SortType[sort];
294 export function routeListingTypeToEnum(type: string): ListingType {
295 return ListingType[type];
298 export function routeDataTypeToEnum(type: string): DataType {
299 return DataType[capitalizeFirstLetter(type)];
302 export function routeSearchTypeToEnum(type: string): SearchType {
303 return SearchType[type];
306 export async function getPageTitle(url: string) {
307 let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
308 let title = await res.title;
312 export function debounce(
315 immediate: boolean = false
317 // 'private' variable for instance
318 // The returned function will be able to reference this due to closure.
319 // Each call to the returned function will share this common timer.
322 // Calling debounce returns a new anonymous function
324 // reference the context and args for the setTimeout function
328 // Should the function be called now? If immediate is true
329 // and not already in a timeout then the answer is: Yes
330 var callNow = immediate && !timeout;
332 // This is the basic debounce behaviour where you can call this
333 // function several times, but it will only execute once
334 // [before or after imposing a delay].
335 // Each time the returned function is called, the timer starts over.
336 clearTimeout(timeout);
338 // Set the new timeout
339 timeout = setTimeout(function () {
340 // Inside the timeout function, clear the timeout variable
341 // which will let the next execution run when in 'immediate' mode
344 // Check if the function already ran with the immediate flag
346 // Call the original function with apply
347 // apply lets you define the 'this' object as well as the arguments
348 // (both captured before setTimeout)
349 func.apply(context, args);
353 // Immediate mode and no wait timer? Execute the function..
354 if (callNow) func.apply(context, args);
359 export function getLanguage(override?: string): string {
360 let user = UserService.Instance.user;
361 let lang = override || (user && user.lang ? user.lang : 'browser');
363 if (lang == 'browser' && isBrowser()) {
364 return getBrowserLanguage();
371 export function getBrowserLanguage(): string {
372 return navigator.language;
375 export function getMomentLanguage(): string {
376 let lang = getLanguage();
377 if (lang.startsWith('zh')) {
379 } else if (lang.startsWith('sv')) {
381 } else if (lang.startsWith('fr')) {
383 } else if (lang.startsWith('de')) {
385 } else if (lang.startsWith('ru')) {
387 } else if (lang.startsWith('es')) {
389 } else if (lang.startsWith('eo')) {
391 } else if (lang.startsWith('nl')) {
393 } else if (lang.startsWith('it')) {
395 } else if (lang.startsWith('fi')) {
397 } else if (lang.startsWith('ca')) {
399 } else if (lang.startsWith('fa')) {
401 } else if (lang.startsWith('pl')) {
403 } else if (lang.startsWith('pt')) {
405 } else if (lang.startsWith('ja')) {
407 } else if (lang.startsWith('ka')) {
409 } else if (lang.startsWith('hi')) {
411 } else if (lang.startsWith('el')) {
413 } else if (lang.startsWith('eu')) {
415 } else if (lang.startsWith('gl')) {
417 } else if (lang.startsWith('tr')) {
419 } else if (lang.startsWith('hu')) {
421 } else if (lang.startsWith('uk')) {
423 } else if (lang.startsWith('sq')) {
425 } else if (lang.startsWith('km')) {
427 } else if (lang.startsWith('ga')) {
429 } else if (lang.startsWith('sr')) {
431 } else if (lang.startsWith('ko')) {
439 export function setTheme(theme: string, forceReload: boolean = false) {
440 if (isBrowser() && (theme !== 'darkly' || forceReload)) {
441 // Unload all the other themes
442 for (var i = 0; i < themes.length; i++) {
443 let styleSheet = document.getElementById(themes[i]);
445 styleSheet.setAttribute('disabled', 'disabled');
450 .getElementById('default-light')
451 .setAttribute('disabled', 'disabled');
453 .getElementById('default-dark')
454 .setAttribute('disabled', 'disabled');
455 // Load the theme dynamically
456 let cssLoc = `/static/assets/css/themes/${theme}.min.css`;
457 loadCss(theme, cssLoc);
458 document.getElementById(theme).removeAttribute('disabled');
462 export function loadCss(id: string, loc: string) {
463 if (!document.getElementById(id)) {
464 var head = document.getElementsByTagName('head')[0];
465 var link = document.createElement('link');
467 link.rel = 'stylesheet';
468 link.type = 'text/css';
471 head.appendChild(link);
475 export function objectFlip(obj: any) {
477 Object.keys(obj).forEach(key => {
483 export function pictrsAvatarThumbnail(src: string): string {
484 // sample url: http://localhost:8535/pictrs/image/thumbnail256/gs7xuu.jpg
485 let split = src.split('/pictrs/image');
486 let out = `${split[0]}/pictrs/image/${
487 canUseWebP() ? 'webp/' : ''
488 }thumbnail96${split[1]}`;
492 export function showAvatars(): boolean {
494 (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
495 !UserService.Instance.user
499 export function isCakeDay(published: string): boolean {
500 // moment(undefined) or moment.utc(undefined) returns the current date/time
501 // moment(null) or moment.utc(null) returns null
502 const userCreationDate = moment.utc(published || null).local();
503 const currentDate = moment(new Date());
506 userCreationDate.date() === currentDate.date() &&
507 userCreationDate.month() === currentDate.month() &&
508 userCreationDate.year() !== currentDate.year()
512 // Converts to image thumbnail
513 export function pictrsImage(hash: string, thumbnail: boolean = false): string {
514 let root = `/pictrs/image`;
516 // Necessary for other servers / domains
517 if (hash.includes('pictrs')) {
518 let split = hash.split('/pictrs/image/');
519 root = `${split[0]}/pictrs/image`;
523 let out = `${root}/${canUseWebP() ? 'webp/' : ''}${
524 thumbnail ? 'thumbnail256/' : ''
529 export function isCommentType(
530 item: Comment | PrivateMessage | Post
533 (item as Comment).community_id !== undefined &&
534 (item as Comment).content !== undefined
538 export function isPostType(
539 item: Comment | PrivateMessage | Post
541 return (item as Post).stickied !== undefined;
544 export function toast(text: string, background: string = 'success') {
545 let backgroundColor = `var(--${background})`;
548 backgroundColor: backgroundColor,
554 export function pictrsDeleteToast(
555 clickToDeleteText: string,
556 deletePictureText: string,
559 let backgroundColor = `var(--light)`;
560 let toast = Toastify({
561 text: clickToDeleteText,
562 backgroundColor: backgroundColor,
568 window.location.replace(deleteUrl);
569 alert(deletePictureText);
577 interface NotifyInfo {
584 export function messageToastify(info: NotifyInfo, router: any) {
585 let htmlBody = info.body ? md.render(info.body) : '';
586 let backgroundColor = `var(--light)`;
588 let toast = Toastify({
589 text: `${htmlBody}<br />${info.name}`,
591 backgroundColor: backgroundColor,
592 className: 'text-dark',
600 router.history.push(info.link);
606 export function notifyPost(post: Post, router: any) {
607 let info: NotifyInfo = {
608 name: post.community_name,
609 icon: post.community_icon ? post.community_icon : defaultFavIcon,
610 link: `/post/${post.id}`,
613 notify(info, router);
616 export function notifyComment(comment: Comment, router: any) {
617 let info: NotifyInfo = {
618 name: comment.creator_name,
619 icon: comment.creator_avatar ? comment.creator_avatar : defaultFavIcon,
620 link: `/post/${comment.post_id}/comment/${comment.id}`,
621 body: comment.content,
623 notify(info, router);
626 export function notifyPrivateMessage(pm: PrivateMessage, router: any) {
627 let info: NotifyInfo = {
628 name: pm.creator_name,
629 icon: pm.creator_avatar ? pm.creator_avatar : defaultFavIcon,
633 notify(info, router);
636 function notify(info: NotifyInfo, router: any) {
637 messageToastify(info, router);
639 if (Notification.permission !== 'granted') Notification.requestPermission();
641 var notification = new Notification(info.name, {
646 notification.onclick = () => {
647 event.preventDefault();
648 router.history.push(info.link);
653 export function setupTribute() {
655 noMatchTemplate: function () {
662 menuItemTemplate: (item: any) => {
663 let shortName = `:${item.original.key}:`;
664 return `${item.original.val} ${shortName}`;
666 selectTemplate: (item: any) => {
667 return `:${item.original.key}:`;
669 values: Object.entries(emojiShortName).map(e => {
670 return { key: e[1], val: e[0] };
673 autocompleteMode: true,
675 // menuItemLimit: mentionDropdownFetchLimit,
676 menuShowMinLength: 2,
681 selectTemplate: (item: any) => {
682 let link = item.original.local
683 ? `[${item.original.key}](/u/${item.original.name})`
684 : `[${item.original.key}](/user/${item.original.id})`;
687 values: (text: string, cb: any) => {
688 userSearch(text, (users: any) => cb(users));
691 autocompleteMode: true,
693 // menuItemLimit: mentionDropdownFetchLimit,
694 menuShowMinLength: 2,
700 selectTemplate: (item: any) => {
701 let link = item.original.local
702 ? `[${item.original.key}](/c/${item.original.name})`
703 : `[${item.original.key}](/community/${item.original.id})`;
706 values: (text: string, cb: any) => {
707 communitySearch(text, (communities: any) => cb(communities));
710 autocompleteMode: true,
712 // menuItemLimit: mentionDropdownFetchLimit,
713 menuShowMinLength: 2,
721 tippyInstance = tippy('[data-tippy-content]');
724 export function setupTippy() {
726 tippyInstance.forEach(e => e.destroy());
727 tippyInstance = tippy('[data-tippy-content]', {
729 // Display on "long press"
730 touch: ['hold', 500],
735 function userSearch(text: string, cb: any) {
737 let form: SearchForm = {
739 type_: SearchType.Users,
740 sort: SortType.TopAll,
742 limit: mentionDropdownFetchLimit,
745 WebSocketService.Instance.search(form);
747 let userSub = WebSocketService.Instance.subject.subscribe(
749 let res = wsJsonToRes(msg);
750 if (res.op == UserOperation.Search) {
751 let data = res.data as SearchResponse;
752 let users = data.users.map(u => {
754 key: `@${u.name}@${hostname(u.actor_id)}`,
761 userSub.unsubscribe();
764 err => console.error(err),
765 () => console.log('complete')
772 function communitySearch(text: string, cb: any) {
774 let form: SearchForm = {
776 type_: SearchType.Communities,
777 sort: SortType.TopAll,
779 limit: mentionDropdownFetchLimit,
782 WebSocketService.Instance.search(form);
784 let communitySub = WebSocketService.Instance.subject.subscribe(
786 let res = wsJsonToRes(msg);
787 if (res.op == UserOperation.Search) {
788 let data = res.data as SearchResponse;
789 let communities = data.communities.map(c => {
791 key: `!${c.name}@${hostname(c.actor_id)}`,
798 communitySub.unsubscribe();
801 err => console.error(err),
802 () => console.log('complete')
809 export function getListingTypeFromProps(props: any): ListingType {
810 return props.match.params.listing_type
811 ? routeListingTypeToEnum(props.match.params.listing_type)
812 : UserService.Instance.user
813 ? Object.values(ListingType)[UserService.Instance.user.default_listing_type]
817 // TODO might need to add a user setting for this too
818 export function getDataTypeFromProps(props: any): DataType {
819 return props.match.params.data_type
820 ? routeDataTypeToEnum(props.match.params.data_type)
824 export function getSortTypeFromProps(props: any): SortType {
825 return props.match.params.sort
826 ? routeSortTypeToEnum(props.match.params.sort)
827 : UserService.Instance.user
828 ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
832 export function getPageFromProps(props: any): number {
833 return props.match.params.page ? Number(props.match.params.page) : 1;
836 export function getRecipientIdFromProps(props: any): number {
837 return props.match.params.recipient_id
838 ? Number(props.match.params.recipient_id)
842 export function getIdFromProps(props: any): number {
843 return Number(props.match.params.id);
846 export function getCommentIdFromProps(props: any): number {
847 return Number(props.match.params.comment_id);
850 export function getUsernameFromProps(props: any): string {
851 return props.match.params.username;
854 export function editCommentRes(data: CommentResponse, comments: Comment[]) {
855 let found = comments.find(c => c.id == data.comment.id);
857 found.content = data.comment.content;
858 found.updated = data.comment.updated;
859 found.removed = data.comment.removed;
860 found.deleted = data.comment.deleted;
861 found.upvotes = data.comment.upvotes;
862 found.downvotes = data.comment.downvotes;
863 found.score = data.comment.score;
867 export function saveCommentRes(data: CommentResponse, comments: Comment[]) {
868 let found = comments.find(c => c.id == data.comment.id);
870 found.saved = data.comment.saved;
874 export function createCommentLikeRes(
875 data: CommentResponse,
878 let found: Comment = comments.find(c => c.id === data.comment.id);
880 found.score = data.comment.score;
881 found.upvotes = data.comment.upvotes;
882 found.downvotes = data.comment.downvotes;
883 if (data.comment.my_vote !== null) {
884 found.my_vote = data.comment.my_vote;
889 export function createPostLikeFindRes(data: PostResponse, posts: Post[]) {
890 let found = posts.find(c => c.id == data.post.id);
892 createPostLikeRes(data, found);
896 export function createPostLikeRes(data: PostResponse, post: Post) {
898 post.score = data.post.score;
899 post.upvotes = data.post.upvotes;
900 post.downvotes = data.post.downvotes;
901 if (data.post.my_vote !== null) {
902 post.my_vote = data.post.my_vote;
907 export function editPostFindRes(data: PostResponse, posts: Post[]) {
908 let found = posts.find(c => c.id == data.post.id);
910 editPostRes(data, found);
914 export function editPostRes(data: PostResponse, post: Post) {
916 post.url = data.post.url;
917 post.name = data.post.name;
918 post.nsfw = data.post.nsfw;
919 post.deleted = data.post.deleted;
920 post.removed = data.post.removed;
921 post.stickied = data.post.stickied;
922 post.body = data.post.body;
923 post.locked = data.post.locked;
927 export function commentsToFlatNodes(comments: Comment[]): CommentNodeI[] {
928 let nodes: CommentNodeI[] = [];
929 for (let comment of comments) {
930 nodes.push({ comment: comment });
935 export function commentSort(tree: CommentNodeI[], sort: CommentSortType) {
936 // First, put removed and deleted comments at the bottom, then do your other sorts
937 if (sort == CommentSortType.Top) {
940 +a.comment.removed - +b.comment.removed ||
941 +a.comment.deleted - +b.comment.deleted ||
942 b.comment.score - a.comment.score
944 } else if (sort == CommentSortType.New) {
947 +a.comment.removed - +b.comment.removed ||
948 +a.comment.deleted - +b.comment.deleted ||
949 b.comment.published.localeCompare(a.comment.published)
951 } else if (sort == CommentSortType.Old) {
954 +a.comment.removed - +b.comment.removed ||
955 +a.comment.deleted - +b.comment.deleted ||
956 a.comment.published.localeCompare(b.comment.published)
958 } else if (sort == CommentSortType.Hot) {
961 +a.comment.removed - +b.comment.removed ||
962 +a.comment.deleted - +b.comment.deleted ||
963 hotRankComment(b.comment) - hotRankComment(a.comment)
967 // Go through the children recursively
968 for (let node of tree) {
970 commentSort(node.children, sort);
975 export function commentSortSortType(tree: CommentNodeI[], sort: SortType) {
976 commentSort(tree, convertCommentSortType(sort));
979 function convertCommentSortType(sort: SortType): CommentSortType {
981 sort == SortType.TopAll ||
982 sort == SortType.TopDay ||
983 sort == SortType.TopWeek ||
984 sort == SortType.TopMonth ||
985 sort == SortType.TopYear
987 return CommentSortType.Top;
988 } else if (sort == SortType.New) {
989 return CommentSortType.New;
990 } else if (sort == SortType.Hot || sort == SortType.Active) {
991 return CommentSortType.Hot;
993 return CommentSortType.Hot;
997 export function postSort(
1000 communityType: boolean
1002 // First, put removed and deleted comments at the bottom, then do your other sorts
1004 sort == SortType.TopAll ||
1005 sort == SortType.TopDay ||
1006 sort == SortType.TopWeek ||
1007 sort == SortType.TopMonth ||
1008 sort == SortType.TopYear
1012 +a.removed - +b.removed ||
1013 +a.deleted - +b.deleted ||
1014 (communityType && +b.stickied - +a.stickied) ||
1017 } else if (sort == SortType.New) {
1020 +a.removed - +b.removed ||
1021 +a.deleted - +b.deleted ||
1022 (communityType && +b.stickied - +a.stickied) ||
1023 b.published.localeCompare(a.published)
1025 } else if (sort == SortType.Hot) {
1028 +a.removed - +b.removed ||
1029 +a.deleted - +b.deleted ||
1030 (communityType && +b.stickied - +a.stickied) ||
1031 b.hot_rank - a.hot_rank
1033 } else if (sort == SortType.Active) {
1036 +a.removed - +b.removed ||
1037 +a.deleted - +b.deleted ||
1038 (communityType && +b.stickied - +a.stickied) ||
1039 b.hot_rank_active - a.hot_rank_active
1044 export const colorList: string[] = [
1053 function hsl(num: number) {
1054 return `hsla(${num}, 35%, 50%, 1)`;
1057 // function randomHsl() {
1058 // return `hsla(${Math.random() * 360}, 100%, 50%, 1)`;
1061 export function previewLines(
1063 maxChars: number = 300,
1064 maxLines: number = 1
1070 // Use lines * 2 because markdown requires 2 lines
1071 .slice(0, maxLines * 2)
1076 export function hostname(url: string): string {
1077 let cUrl = new URL(url);
1078 return cUrl.port ? `${cUrl.hostname}:${cUrl.port}` : `${cUrl.hostname}`;
1081 function canUseWebP() {
1082 // TODO pictshare might have a webp conversion bug, try disabling this
1085 // var elem = document.createElement('canvas');
1086 // if (!!(elem.getContext && elem.getContext('2d'))) {
1087 // var testString = !(window.mozInnerScreenX == null) ? 'png' : 'webp';
1088 // // was able or not to get WebP representation
1090 // elem.toDataURL('image/webp').startsWith('data:image/' + testString)
1094 // // very old browser like IE 8, canvas not supported
1098 export function validTitle(title?: string): boolean {
1099 // Initial title is null, minimum length is taken care of by textarea's minLength={3}
1100 if (title === null || title.length < 3) return true;
1102 const regex = new RegExp(/.*\S.*/, 'g');
1104 return regex.test(title);
1107 export function siteBannerCss(banner: string): string {
1109 background-image: linear-gradient( rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8) ) ,url("${banner}"); \
1110 background-attachment: fixed; \
1111 background-position: top; \
1112 background-repeat: no-repeat; \
1113 background-size: 100% cover; \
1116 max-height: 100vh; \
1120 export function isBrowser() {
1121 return typeof window !== 'undefined';
1124 export function setAuth(obj: any, auth: string) {
1130 export function setIsoData(context: any): IsoData {
1131 let isoData: IsoData = isBrowser()
1133 : context.router.staticContext;
1137 export function wsSubscribe(parseMessage: any): Subscription {
1139 return WebSocketService.Instance.subject
1140 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
1142 msg => parseMessage(msg),
1143 err => console.error(err),
1144 () => console.log('complete')