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';
32 CommentNode as CommentNodeI,
40 WebSocketJsonResponse,
46 } from 'lemmy-js-client';
48 import { httpUri } from './env';
50 import { CommentSortType, DataType } from './interfaces';
51 import { UserService, WebSocketService } from './services';
53 // import Tribute from 'tributejs';
54 import markdown_it from 'markdown-it';
55 import markdown_it_sub from 'markdown-it-sub';
56 import markdown_it_sup from 'markdown-it-sup';
57 import markdownitEmoji from 'markdown-it-emoji/light';
58 import markdown_it_container from 'markdown-it-container';
59 import emojiShortName from 'emoji-short-name';
60 import Toastify from 'toastify-js';
61 import tippy from 'tippy.js';
62 import moment from 'moment';
64 export const favIconUrl = '/static/assets/favicon.svg';
65 export const favIconPngUrl = '/static/assets/apple-touch-icon.png';
67 // export const defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`;
68 export const defaultFavIcon = 'test';
69 export const repoUrl = 'https://github.com/LemmyNet/lemmy';
70 export const helpGuideUrl = '/docs/about_guide.html';
71 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
72 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
73 export const archiveUrl = 'https://archive.is';
74 export const elementUrl = 'https://element.io/';
76 export const postRefetchSeconds: number = 60 * 1000;
77 export const fetchLimit: number = 20;
78 export const mentionDropdownFetchLimit = 10;
80 export const lemmyHttp = new LemmyHttp(httpUri);
82 export const languages = [
83 { code: 'ca', name: 'Català' },
84 { code: 'en', name: 'English' },
85 { code: 'el', name: 'Ελληνικά' },
86 { code: 'eu', name: 'Euskara' },
87 { code: 'eo', name: 'Esperanto' },
88 { code: 'es', name: 'Español' },
89 { code: 'de', name: 'Deutsch' },
90 { code: 'ga', name: 'Gaeilge' },
91 { code: 'gl', name: 'Galego' },
92 { code: 'hu', name: 'Magyar Nyelv' },
93 { code: 'ka', name: 'ქართული ენა' },
94 { code: 'km', name: 'ភាសាខ្មែរ' },
95 { code: 'hi', name: 'मानक हिन्दी' },
96 { code: 'fa', name: 'فارسی' },
97 { code: 'ja', name: '日本語' },
98 { code: 'pl', name: 'Polski' },
99 { code: 'pt_BR', name: 'Português Brasileiro' },
100 { code: 'zh', name: '中文' },
101 { code: 'fi', name: 'Suomi' },
102 { code: 'fr', name: 'Français' },
103 { code: 'sv', name: 'Svenska' },
104 { code: 'sq', name: 'Shqip' },
105 { code: 'sr_Latn', name: 'srpski' },
106 { code: 'tr', name: 'Türkçe' },
107 { code: 'uk', name: 'Українська Mова' },
108 { code: 'ru', name: 'Русский' },
109 { code: 'nl', name: 'Nederlands' },
110 { code: 'it', name: 'Italiano' },
113 export const themes = [
129 const DEFAULT_ALPHABET =
130 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
132 function getRandomCharFromAlphabet(alphabet: string): string {
133 return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
136 export function randomStr(
137 idDesiredLength: number = 20,
138 alphabet = DEFAULT_ALPHABET
141 * Create n-long array and map it to random chars from given alphabet.
142 * Then join individual chars as string
144 return Array.from({ length: idDesiredLength })
146 return getRandomCharFromAlphabet(alphabet);
151 export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
152 let opStr: string = msg.op;
154 op: UserOperation[opStr],
159 export const md = new markdown_it({
164 .use(markdown_it_sub)
165 .use(markdown_it_sup)
166 .use(markdown_it_container, 'spoiler', {
167 validate: function (params: any) {
168 return params.trim().match(/^spoiler\s+(.*)$/);
171 render: function (tokens: any, idx: any) {
172 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
174 if (tokens[idx].nesting === 1) {
176 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
179 return '</details>\n';
183 .use(markdownitEmoji, {
184 defs: objectFlip(emojiShortName),
187 export function hotRankComment(comment: Comment): number {
188 return hotRank(comment.score, comment.published);
191 export function hotRankPost(post: Post): number {
192 return hotRank(post.score, post.newest_activity_time);
195 export function hotRank(score: number, timeStr: string): number {
196 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
197 let date: Date = new Date(timeStr + 'Z'); // Add Z to convert from UTC date
198 let now: Date = new Date();
199 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
202 (10000 * Math.log10(Math.max(1, 3 + score))) /
203 Math.pow(hoursElapsed + 2, 1.8);
205 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
210 export function mdToHtml(text: string) {
211 return { __html: md.render(text) };
214 export function getUnixTime(text: string): number {
215 return text ? new Date(text).getTime() / 1000 : undefined;
218 export function addTypeInfo<T>(
221 ): { type_: string; data: T }[] {
222 return arr.map(e => {
223 return { type_: name, data: e };
227 export function canMod(
231 onSelf: boolean = false
233 // You can do moderator actions only on the mods added after you.
235 let yourIndex = modIds.findIndex(id => id == user.id);
236 if (yourIndex == -1) {
239 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
240 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
241 return !modIds.includes(creator_id);
248 export function isMod(modIds: number[], creator_id: number): boolean {
249 return modIds.includes(creator_id);
252 const imageRegex = new RegExp(
253 /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/
255 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
257 export function isImage(url: string) {
258 return imageRegex.test(url);
261 export function isVideo(url: string) {
262 return videoRegex.test(url);
266 export function validURL(str: string) {
268 return !!new URL(str);
274 export function validEmail(email: string) {
275 let re = /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
276 return re.test(String(email).toLowerCase());
279 export function capitalizeFirstLetter(str: string): string {
280 return str.charAt(0).toUpperCase() + str.slice(1);
283 export function routeSortTypeToEnum(sort: string): SortType {
284 return SortType[sort];
287 export function routeListingTypeToEnum(type: string): ListingType {
288 return ListingType[type];
291 export function routeDataTypeToEnum(type: string): DataType {
292 return DataType[capitalizeFirstLetter(type)];
295 export function routeSearchTypeToEnum(type: string): SearchType {
296 return SearchType[capitalizeFirstLetter(type)];
299 export async function getPageTitle(url: string) {
300 let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
301 let title = await res.title;
305 export function debounce(
308 immediate: boolean = false
310 // 'private' variable for instance
311 // The returned function will be able to reference this due to closure.
312 // Each call to the returned function will share this common timer.
315 // Calling debounce returns a new anonymous function
317 // reference the context and args for the setTimeout function
321 // Should the function be called now? If immediate is true
322 // and not already in a timeout then the answer is: Yes
323 var callNow = immediate && !timeout;
325 // This is the basic debounce behaviour where you can call this
326 // function several times, but it will only execute once
327 // [before or after imposing a delay].
328 // Each time the returned function is called, the timer starts over.
329 clearTimeout(timeout);
331 // Set the new timeout
332 timeout = setTimeout(function () {
333 // Inside the timeout function, clear the timeout variable
334 // which will let the next execution run when in 'immediate' mode
337 // Check if the function already ran with the immediate flag
339 // Call the original function with apply
340 // apply lets you define the 'this' object as well as the arguments
341 // (both captured before setTimeout)
342 func.apply(context, args);
346 // Immediate mode and no wait timer? Execute the function..
347 if (callNow) func.apply(context, args);
352 export function getLanguage(override?: string): string {
354 // let user = UserService.Instance.user;
355 // let lang = override || (user && user.lang ? user.lang : 'browser');
357 // if (lang == 'browser') {
358 // return getBrowserLanguage();
365 export function getBrowserLanguage(): string {
366 return navigator.language;
369 export function getMomentLanguage(): string {
370 let lang = getLanguage();
371 if (lang.startsWith('zh')) {
373 } else if (lang.startsWith('sv')) {
375 } else if (lang.startsWith('fr')) {
377 } else if (lang.startsWith('de')) {
379 } else if (lang.startsWith('ru')) {
381 } else if (lang.startsWith('es')) {
383 } else if (lang.startsWith('eo')) {
385 } else if (lang.startsWith('nl')) {
387 } else if (lang.startsWith('it')) {
389 } else if (lang.startsWith('fi')) {
391 } else if (lang.startsWith('ca')) {
393 } else if (lang.startsWith('fa')) {
395 } else if (lang.startsWith('pl')) {
397 } else if (lang.startsWith('pt')) {
399 } else if (lang.startsWith('ja')) {
401 } else if (lang.startsWith('ka')) {
403 } else if (lang.startsWith('hi')) {
405 } else if (lang.startsWith('el')) {
407 } else if (lang.startsWith('eu')) {
409 } else if (lang.startsWith('gl')) {
411 } else if (lang.startsWith('tr')) {
413 } else if (lang.startsWith('hu')) {
415 } else if (lang.startsWith('uk')) {
417 } else if (lang.startsWith('sq')) {
419 } else if (lang.startsWith('km')) {
421 } else if (lang.startsWith('ga')) {
423 } else if (lang.startsWith('sr')) {
432 export function setTheme(theme: string = 'darkly', loggedIn: boolean = false) {
433 // unload all the other themes
434 // for (var i = 0; i < themes.length; i++) {
435 // let styleSheet = document.getElementById(themes[i]);
437 // styleSheet.setAttribute('disabled', 'disabled');
440 // // if the user is not logged in, we load the default themes and let the browser decide
442 // document.getElementById('default-light').removeAttribute('disabled');
443 // document.getElementById('default-dark').removeAttribute('disabled');
446 // .getElementById('default-light')
447 // .setAttribute('disabled', 'disabled');
449 // .getElementById('default-dark')
450 // .setAttribute('disabled', 'disabled');
451 // // Load the theme dynamically
452 // let cssLoc = `/static/assets/css/themes/${theme}.min.css`;
453 // loadCss(theme, cssLoc);
454 // document.getElementById(theme).removeAttribute('disabled');
458 // export function loadCss(id: string, loc: string) {
459 // if (!document.getElementById(id)) {
460 // var head = document.getElementsByTagName('head')[0];
461 // var link = document.createElement('link');
463 // link.rel = 'stylesheet';
464 // link.type = 'text/css';
466 // link.media = 'all';
467 // head.appendChild(link);
471 export function objectFlip(obj: any) {
473 Object.keys(obj).forEach(key => {
479 export function pictrsAvatarThumbnail(src: string): string {
480 // sample url: http://localhost:8535/pictrs/image/thumbnail256/gs7xuu.jpg
481 let split = src.split('/pictrs/image');
482 let out = `${split[0]}/pictrs/image/${
483 canUseWebP() ? 'webp/' : ''
484 }thumbnail96${split[1]}`;
488 export function showAvatars(): boolean {
490 (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
491 !UserService.Instance.user
495 export function isCakeDay(published: string): boolean {
496 // moment(undefined) or moment.utc(undefined) returns the current date/time
497 // moment(null) or moment.utc(null) returns null
498 const userCreationDate = moment.utc(published || null).local();
499 const currentDate = moment(new Date());
502 userCreationDate.date() === currentDate.date() &&
503 userCreationDate.month() === currentDate.month() &&
504 userCreationDate.year() !== currentDate.year()
508 // Converts to image thumbnail
509 export function pictrsImage(hash: string, thumbnail: boolean = false): string {
510 let root = `/pictrs/image`;
512 // Necessary for other servers / domains
513 if (hash.includes('pictrs')) {
514 let split = hash.split('/pictrs/image/');
515 root = `${split[0]}/pictrs/image`;
519 let out = `${root}/${canUseWebP() ? 'webp/' : ''}${
520 thumbnail ? 'thumbnail256/' : ''
525 export function isCommentType(
526 item: Comment | PrivateMessage | Post
529 (item as Comment).community_id !== undefined &&
530 (item as Comment).content !== undefined
534 export function isPostType(
535 item: Comment | PrivateMessage | Post
537 return (item as Post).stickied !== undefined;
540 export function toast(text: string, background: string = 'success') {
541 let backgroundColor = `var(--${background})`;
544 backgroundColor: backgroundColor,
550 export function pictrsDeleteToast(
551 clickToDeleteText: string,
552 deletePictureText: string,
555 let backgroundColor = `var(--light)`;
556 let toast = Toastify({
557 text: clickToDeleteText,
558 backgroundColor: backgroundColor,
564 window.location.replace(deleteUrl);
565 alert(deletePictureText);
573 interface NotifyInfo {
580 export function messageToastify(info: NotifyInfo, router: any) {
581 let htmlBody = info.body ? md.render(info.body) : '';
582 let backgroundColor = `var(--light)`;
584 let toast = Toastify({
585 text: `${htmlBody}<br />${info.name}`,
587 backgroundColor: backgroundColor,
588 className: 'text-dark',
596 router.history.push(info.link);
602 export function notifyPost(post: Post, router: any) {
603 let info: NotifyInfo = {
604 name: post.community_name,
605 icon: post.community_icon ? post.community_icon : defaultFavIcon,
606 link: `/post/${post.id}`,
609 notify(info, router);
612 export function notifyComment(comment: Comment, router: any) {
613 let info: NotifyInfo = {
614 name: comment.creator_name,
615 icon: comment.creator_avatar ? comment.creator_avatar : defaultFavIcon,
616 link: `/post/${comment.post_id}/comment/${comment.id}`,
617 body: comment.content,
619 notify(info, router);
622 export function notifyPrivateMessage(pm: PrivateMessage, router: any) {
623 let info: NotifyInfo = {
624 name: pm.creator_name,
625 icon: pm.creator_avatar ? pm.creator_avatar : defaultFavIcon,
629 notify(info, router);
632 function notify(info: NotifyInfo, router: any) {
633 messageToastify(info, router);
635 if (Notification.permission !== 'granted') Notification.requestPermission();
637 var notification = new Notification(info.name, {
642 notification.onclick = () => {
643 event.preventDefault();
644 router.history.push(info.link);
649 // export function setupTribute(): Tribute<{}> {
650 // return new Tribute({
651 // noMatchTemplate: function () {
658 // menuItemTemplate: (item: any) => {
659 // let shortName = `:${item.original.key}:`;
660 // return `${item.original.val} ${shortName}`;
662 // selectTemplate: (item: any) => {
663 // return `:${item.original.key}:`;
665 // values: Object.entries(emojiShortName).map(e => {
666 // return { key: e[1], val: e[0] };
668 // allowSpaces: false,
669 // autocompleteMode: true,
671 // // menuItemLimit: mentionDropdownFetchLimit,
672 // menuShowMinLength: 2,
677 // selectTemplate: (item: any) => {
678 // let link = item.original.local
679 // ? `[${item.original.key}](/u/${item.original.name})`
680 // : `[${item.original.key}](/user/${item.original.id})`;
683 // values: (text: string, cb: any) => {
684 // userSearch(text, (users: any) => cb(users));
686 // allowSpaces: false,
687 // autocompleteMode: true,
689 // // menuItemLimit: mentionDropdownFetchLimit,
690 // menuShowMinLength: 2,
696 // selectTemplate: (item: any) => {
697 // let link = item.original.local
698 // ? `[${item.original.key}](/c/${item.original.name})`
699 // : `[${item.original.key}](/community/${item.original.id})`;
702 // values: (text: string, cb: any) => {
703 // communitySearch(text, (communities: any) => cb(communities));
705 // allowSpaces: false,
706 // autocompleteMode: true,
708 // // menuItemLimit: mentionDropdownFetchLimit,
709 // menuShowMinLength: 2,
716 // let tippyInstance = tippy('[data-tippy-content]');
718 export function setupTippy() {
719 // tippyInstance.forEach(e => e.destroy());
720 // tippyInstance = tippy('[data-tippy-content]', {
722 // // Display on "long press"
723 // touch: ['hold', 500],
727 function userSearch(text: string, cb: any) {
729 let form: SearchForm = {
731 type_: SearchType.Users,
732 sort: SortType.TopAll,
734 limit: mentionDropdownFetchLimit,
737 WebSocketService.Instance.search(form);
739 let userSub = WebSocketService.Instance.subject.subscribe(
741 let res = wsJsonToRes(msg);
742 if (res.op == UserOperation.Search) {
743 let data = res.data as SearchResponse;
744 let users = data.users.map(u => {
746 key: `@${u.name}@${hostname(u.actor_id)}`,
753 userSub.unsubscribe();
756 err => console.error(err),
757 () => console.log('complete')
764 function communitySearch(text: string, cb: any) {
766 let form: SearchForm = {
768 type_: SearchType.Communities,
769 sort: SortType.TopAll,
771 limit: mentionDropdownFetchLimit,
774 WebSocketService.Instance.search(form);
776 let communitySub = WebSocketService.Instance.subject.subscribe(
778 let res = wsJsonToRes(msg);
779 if (res.op == UserOperation.Search) {
780 let data = res.data as SearchResponse;
781 let communities = data.communities.map(c => {
783 key: `!${c.name}@${hostname(c.actor_id)}`,
790 communitySub.unsubscribe();
793 err => console.error(err),
794 () => console.log('complete')
801 export function getListingTypeFromProps(props: any): ListingType {
803 return ListingType.All;
804 // return props.match.params.listing_type
805 // ? routeListingTypeToEnum(props.match.params.listing_type)
806 // : UserService.Instance.user
807 // ? Object.values(ListingType)[UserService.Instance.user.default_listing_type]
808 // : ListingType.All;
811 // TODO might need to add a user setting for this too
812 export function getDataTypeFromProps(props: any): DataType {
814 return DataType.Post;
815 // return props.match.params.data_type
816 // ? routeDataTypeToEnum(props.match.params.data_type)
820 export function getSortTypeFromProps(props: any): SortType {
822 return SortType.Active;
823 // return props.match.params.sort
824 // ? routeSortTypeToEnum(props.match.params.sort)
825 // : UserService.Instance.user
826 // ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
827 // : SortType.Active;
830 export function getPageFromProps(props: any): number {
833 // return props.match.params.page ? Number(props.match.params.page) : 1;
836 export function editCommentRes(data: CommentResponse, comments: Comment[]) {
837 let found = comments.find(c => c.id == data.comment.id);
839 found.content = data.comment.content;
840 found.updated = data.comment.updated;
841 found.removed = data.comment.removed;
842 found.deleted = data.comment.deleted;
843 found.upvotes = data.comment.upvotes;
844 found.downvotes = data.comment.downvotes;
845 found.score = data.comment.score;
849 export function saveCommentRes(data: CommentResponse, comments: Comment[]) {
850 let found = comments.find(c => c.id == data.comment.id);
852 found.saved = data.comment.saved;
856 export function createCommentLikeRes(
857 data: CommentResponse,
860 let found: Comment = comments.find(c => c.id === data.comment.id);
862 found.score = data.comment.score;
863 found.upvotes = data.comment.upvotes;
864 found.downvotes = data.comment.downvotes;
865 if (data.comment.my_vote !== null) {
866 found.my_vote = data.comment.my_vote;
871 export function createPostLikeFindRes(data: PostResponse, posts: Post[]) {
872 let found = posts.find(c => c.id == data.post.id);
874 createPostLikeRes(data, found);
878 export function createPostLikeRes(data: PostResponse, post: Post) {
880 post.score = data.post.score;
881 post.upvotes = data.post.upvotes;
882 post.downvotes = data.post.downvotes;
883 if (data.post.my_vote !== null) {
884 post.my_vote = data.post.my_vote;
889 export function editPostFindRes(data: PostResponse, posts: Post[]) {
890 let found = posts.find(c => c.id == data.post.id);
892 editPostRes(data, found);
896 export function editPostRes(data: PostResponse, post: Post) {
898 post.url = data.post.url;
899 post.name = data.post.name;
900 post.nsfw = data.post.nsfw;
901 post.deleted = data.post.deleted;
902 post.removed = data.post.removed;
903 post.stickied = data.post.stickied;
904 post.body = data.post.body;
905 post.locked = data.post.locked;
909 export function commentsToFlatNodes(comments: Comment[]): CommentNodeI[] {
910 let nodes: CommentNodeI[] = [];
911 for (let comment of comments) {
912 nodes.push({ comment: comment });
917 export function commentSort(tree: CommentNodeI[], sort: CommentSortType) {
918 // First, put removed and deleted comments at the bottom, then do your other sorts
919 if (sort == CommentSortType.Top) {
922 +a.comment.removed - +b.comment.removed ||
923 +a.comment.deleted - +b.comment.deleted ||
924 b.comment.score - a.comment.score
926 } else if (sort == CommentSortType.New) {
929 +a.comment.removed - +b.comment.removed ||
930 +a.comment.deleted - +b.comment.deleted ||
931 b.comment.published.localeCompare(a.comment.published)
933 } else if (sort == CommentSortType.Old) {
936 +a.comment.removed - +b.comment.removed ||
937 +a.comment.deleted - +b.comment.deleted ||
938 a.comment.published.localeCompare(b.comment.published)
940 } else if (sort == CommentSortType.Hot) {
943 +a.comment.removed - +b.comment.removed ||
944 +a.comment.deleted - +b.comment.deleted ||
945 hotRankComment(b.comment) - hotRankComment(a.comment)
949 // Go through the children recursively
950 for (let node of tree) {
952 commentSort(node.children, sort);
957 export function commentSortSortType(tree: CommentNodeI[], sort: SortType) {
958 commentSort(tree, convertCommentSortType(sort));
961 function convertCommentSortType(sort: SortType): CommentSortType {
963 sort == SortType.TopAll ||
964 sort == SortType.TopDay ||
965 sort == SortType.TopWeek ||
966 sort == SortType.TopMonth ||
967 sort == SortType.TopYear
969 return CommentSortType.Top;
970 } else if (sort == SortType.New) {
971 return CommentSortType.New;
972 } else if (sort == SortType.Hot || sort == SortType.Active) {
973 return CommentSortType.Hot;
975 return CommentSortType.Hot;
979 export function postSort(
982 communityType: boolean
984 // First, put removed and deleted comments at the bottom, then do your other sorts
986 sort == SortType.TopAll ||
987 sort == SortType.TopDay ||
988 sort == SortType.TopWeek ||
989 sort == SortType.TopMonth ||
990 sort == SortType.TopYear
994 +a.removed - +b.removed ||
995 +a.deleted - +b.deleted ||
996 (communityType && +b.stickied - +a.stickied) ||
999 } else if (sort == SortType.New) {
1002 +a.removed - +b.removed ||
1003 +a.deleted - +b.deleted ||
1004 (communityType && +b.stickied - +a.stickied) ||
1005 b.published.localeCompare(a.published)
1007 } else if (sort == SortType.Hot) {
1010 +a.removed - +b.removed ||
1011 +a.deleted - +b.deleted ||
1012 (communityType && +b.stickied - +a.stickied) ||
1013 b.hot_rank - a.hot_rank
1015 } else if (sort == SortType.Active) {
1018 +a.removed - +b.removed ||
1019 +a.deleted - +b.deleted ||
1020 (communityType && +b.stickied - +a.stickied) ||
1021 b.hot_rank_active - a.hot_rank_active
1026 export const colorList: string[] = [
1035 function hsl(num: number) {
1036 return `hsla(${num}, 35%, 50%, 1)`;
1039 function randomHsl() {
1040 return `hsla(${Math.random() * 360}, 100%, 50%, 1)`;
1043 export function previewLines(
1045 maxChars: number = 300,
1046 maxLines: number = 1
1052 // Use lines * 2 because markdown requires 2 lines
1053 .slice(0, maxLines * 2)
1058 export function hostname(url: string): string {
1059 let cUrl = new URL(url);
1061 return `${cUrl.hostname}:${cUrl.port}`;
1062 // return window.location.port
1063 // ? `${cUrl.hostname}:${cUrl.port}`
1064 // : `${cUrl.hostname}`;
1067 function canUseWebP() {
1068 // TODO pictshare might have a webp conversion bug, try disabling this
1071 // var elem = document.createElement('canvas');
1072 // if (!!(elem.getContext && elem.getContext('2d'))) {
1073 // var testString = !(window.mozInnerScreenX == null) ? 'png' : 'webp';
1074 // // was able or not to get WebP representation
1076 // elem.toDataURL('image/webp').startsWith('data:image/' + testString)
1080 // // very old browser like IE 8, canvas not supported
1084 export function validTitle(title?: string): boolean {
1085 // Initial title is null, minimum length is taken care of by textarea's minLength={3}
1086 if (title === null || title.length < 3) return true;
1088 const regex = new RegExp(/.*\S.*/, 'g');
1090 return regex.test(title);
1093 export function siteBannerCss(banner: string): string {
1095 background-image: linear-gradient( rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8) ) ,url("${banner}"); \
1096 background-attachment: fixed; \
1097 background-position: top; \
1098 background-repeat: no-repeat; \
1099 background-size: 100% cover; \
1102 max-height: 100vh; \
1106 export function isBrowser() {
1107 return typeof window !== 'undefined';
1110 export function setAuth(obj: any, auth: string) {