feat: Add date/time template options to template titles
This commit is contained in:
@@ -11,7 +11,7 @@ import ArrowKeyNavigation from "~/components/ArrowKeyNavigation";
|
|||||||
import DelayedMount from "~/components/DelayedMount";
|
import DelayedMount from "~/components/DelayedMount";
|
||||||
import PlaceholderList from "~/components/List/Placeholder";
|
import PlaceholderList from "~/components/List/Placeholder";
|
||||||
import withStores from "~/components/withStores";
|
import withStores from "~/components/withStores";
|
||||||
import { dateToHeading } from "~/utils/dates";
|
import { dateToHeading } from "~/utils/date";
|
||||||
|
|
||||||
export interface PaginatedItem {
|
export interface PaginatedItem {
|
||||||
id: string;
|
id: string;
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ import Button from "~/components/Button";
|
|||||||
import ContextMenu from "~/components/ContextMenu";
|
import ContextMenu from "~/components/ContextMenu";
|
||||||
import MenuItem from "~/components/ContextMenu/MenuItem";
|
import MenuItem from "~/components/ContextMenu/MenuItem";
|
||||||
import Separator from "~/components/ContextMenu/Separator";
|
import Separator from "~/components/ContextMenu/Separator";
|
||||||
|
import useCurrentUser from "~/hooks/useCurrentUser";
|
||||||
import useStores from "~/hooks/useStores";
|
import useStores from "~/hooks/useStores";
|
||||||
|
import { replaceTitleVariables } from "~/utils/date";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
document: Document;
|
document: Document;
|
||||||
@@ -20,6 +22,7 @@ function TemplatesMenu({ onSelectTemplate, document }: Props) {
|
|||||||
const menu = useMenuState({
|
const menu = useMenuState({
|
||||||
modal: true,
|
modal: true,
|
||||||
});
|
});
|
||||||
|
const user = useCurrentUser();
|
||||||
const { documents } = useStores();
|
const { documents } = useStores();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const templates = documents.templates;
|
const templates = documents.templates;
|
||||||
@@ -43,7 +46,9 @@ function TemplatesMenu({ onSelectTemplate, document }: Props) {
|
|||||||
{...menu}
|
{...menu}
|
||||||
>
|
>
|
||||||
<TemplateItem>
|
<TemplateItem>
|
||||||
<strong>{template.titleWithDefault}</strong>
|
<strong>
|
||||||
|
{replaceTitleVariables(template.titleWithDefault, user)}
|
||||||
|
</strong>
|
||||||
<br />
|
<br />
|
||||||
<Author>
|
<Author>
|
||||||
{t("By {{ author }}", {
|
{t("By {{ author }}", {
|
||||||
@@ -76,6 +81,9 @@ function TemplatesMenu({ onSelectTemplate, document }: Props) {
|
|||||||
|
|
||||||
const TemplateItem = styled.div`
|
const TemplateItem = styled.div`
|
||||||
text-align: left;
|
text-align: left;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const Author = styled.div`
|
const Author = styled.div`
|
||||||
|
|||||||
@@ -23,10 +23,6 @@ export default class Document extends ParanoidModel {
|
|||||||
constructor(fields: Record<string, any>, store: DocumentsStore) {
|
constructor(fields: Record<string, any>, store: DocumentsStore) {
|
||||||
super(fields, store);
|
super(fields, store);
|
||||||
|
|
||||||
if (this.isPersistedOnce && this.isFromTemplate) {
|
|
||||||
this.title = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
this.embedsDisabled = Storage.get(`embedsDisabled-${this.id}`) ?? false;
|
this.embedsDisabled = Storage.get(`embedsDisabled-${this.id}`) ?? false;
|
||||||
|
|
||||||
autorun(() => {
|
autorun(() => {
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import withStores from "~/components/withStores";
|
|||||||
import type { Editor as TEditor } from "~/editor";
|
import type { Editor as TEditor } from "~/editor";
|
||||||
import { NavigationNode } from "~/types";
|
import { NavigationNode } from "~/types";
|
||||||
import { client } from "~/utils/ApiClient";
|
import { client } from "~/utils/ApiClient";
|
||||||
|
import { replaceTitleVariables } from "~/utils/date";
|
||||||
import { emojiToUrl } from "~/utils/emoji";
|
import { emojiToUrl } from "~/utils/emoji";
|
||||||
import { isModKey } from "~/utils/keyboard";
|
import { isModKey } from "~/utils/keyboard";
|
||||||
import {
|
import {
|
||||||
@@ -186,8 +187,12 @@ class DocumentScene extends React.Component<Props> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!this.title) {
|
if (!this.title) {
|
||||||
this.title = template.title;
|
const title = replaceTitleVariables(
|
||||||
this.props.document.title = template.title;
|
template.title,
|
||||||
|
this.props.auth.user || undefined
|
||||||
|
);
|
||||||
|
this.title = title;
|
||||||
|
this.props.document.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.props.document.text = template.text;
|
this.props.document.text = template.text;
|
||||||
|
|||||||
@@ -6,7 +6,15 @@ import {
|
|||||||
differenceInCalendarYears,
|
differenceInCalendarYears,
|
||||||
format as formatDate,
|
format as formatDate,
|
||||||
} from "date-fns";
|
} from "date-fns";
|
||||||
|
import { startCase } from "lodash";
|
||||||
import { TFunction } from "react-i18next";
|
import { TFunction } from "react-i18next";
|
||||||
|
import {
|
||||||
|
getCurrentDateAsString,
|
||||||
|
getCurrentDateTimeAsString,
|
||||||
|
getCurrentTimeAsString,
|
||||||
|
unicodeCLDRtoBCP47,
|
||||||
|
} from "@shared/utils/date";
|
||||||
|
import User from "~/models/User";
|
||||||
import { dateLocale } from "~/utils/i18n";
|
import { dateLocale } from "~/utils/i18n";
|
||||||
|
|
||||||
export function dateToHeading(
|
export function dateToHeading(
|
||||||
@@ -62,3 +70,21 @@ export function dateToHeading(
|
|||||||
locale,
|
locale,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces template variables in the given text with the current date and time.
|
||||||
|
*
|
||||||
|
* @param text The text to replace the variables in
|
||||||
|
* @param user The user to get the language/locale from
|
||||||
|
* @returns The text with the variables replaced
|
||||||
|
*/
|
||||||
|
export function replaceTitleVariables(text: string, user?: User) {
|
||||||
|
const locales = user?.language
|
||||||
|
? unicodeCLDRtoBCP47(user.language)
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
return text
|
||||||
|
.replace("{date}", startCase(getCurrentDateAsString(locales)))
|
||||||
|
.replace("{time}", startCase(getCurrentTimeAsString(locales)))
|
||||||
|
.replace("{datetime}", startCase(getCurrentDateTimeAsString(locales)));
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { i18n } from "i18next";
|
import { i18n } from "i18next";
|
||||||
|
import { unicodeCLDRtoBCP47 } from "@shared/utils/date";
|
||||||
import Desktop from "./Desktop";
|
import Desktop from "./Desktop";
|
||||||
|
|
||||||
export function detectLanguage() {
|
export function detectLanguage() {
|
||||||
@@ -14,7 +15,7 @@ export function changeLanguage(
|
|||||||
if (toLanguageString && i18n.language !== toLanguageString) {
|
if (toLanguageString && i18n.language !== toLanguageString) {
|
||||||
// Languages are stored in en_US format in the database, however the
|
// Languages are stored in en_US format in the database, however the
|
||||||
// frontend translation framework (i18next) expects en-US
|
// frontend translation framework (i18next) expects en-US
|
||||||
const locale = toLanguageString.replace("_", "-");
|
const locale = unicodeCLDRtoBCP47(toLanguageString);
|
||||||
i18n.changeLanguage(locale);
|
i18n.changeLanguage(locale);
|
||||||
|
|
||||||
Desktop.bridge?.setSpellCheckerLanguages(["en-US", locale]);
|
Desktop.bridge?.setSpellCheckerLanguages(["en-US", locale]);
|
||||||
|
|||||||
@@ -1,5 +1,26 @@
|
|||||||
import { Transaction } from "sequelize";
|
import { Transaction } from "sequelize";
|
||||||
import { Document, Event, User } from "@server/models";
|
import { Document, Event, User } from "@server/models";
|
||||||
|
import DocumentHelper from "@server/models/helpers/DocumentHelper";
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
id?: string;
|
||||||
|
title: string;
|
||||||
|
text: string;
|
||||||
|
publish?: boolean;
|
||||||
|
collectionId?: string;
|
||||||
|
parentDocumentId?: string | null;
|
||||||
|
importId?: string;
|
||||||
|
templateDocument?: Document | null;
|
||||||
|
publishedAt?: Date;
|
||||||
|
template?: boolean;
|
||||||
|
createdAt?: Date;
|
||||||
|
updatedAt?: Date;
|
||||||
|
user: User;
|
||||||
|
editorVersion?: string;
|
||||||
|
source?: "import";
|
||||||
|
ip?: string;
|
||||||
|
transaction: Transaction;
|
||||||
|
};
|
||||||
|
|
||||||
export default async function documentCreator({
|
export default async function documentCreator({
|
||||||
title = "",
|
title = "",
|
||||||
@@ -20,25 +41,7 @@ export default async function documentCreator({
|
|||||||
source,
|
source,
|
||||||
ip,
|
ip,
|
||||||
transaction,
|
transaction,
|
||||||
}: {
|
}: Props): Promise<Document> {
|
||||||
id?: string;
|
|
||||||
title: string;
|
|
||||||
text: string;
|
|
||||||
publish?: boolean;
|
|
||||||
collectionId?: string;
|
|
||||||
parentDocumentId?: string | null;
|
|
||||||
importId?: string;
|
|
||||||
templateDocument?: Document | null;
|
|
||||||
publishedAt?: Date;
|
|
||||||
template?: boolean;
|
|
||||||
createdAt?: Date;
|
|
||||||
updatedAt?: Date;
|
|
||||||
user: User;
|
|
||||||
editorVersion?: string;
|
|
||||||
source?: "import";
|
|
||||||
ip?: string;
|
|
||||||
transaction: Transaction;
|
|
||||||
}): Promise<Document> {
|
|
||||||
const templateId = templateDocument ? templateDocument.id : undefined;
|
const templateId = templateDocument ? templateDocument.id : undefined;
|
||||||
const document = await Document.create(
|
const document = await Document.create(
|
||||||
{
|
{
|
||||||
@@ -56,7 +59,9 @@ export default async function documentCreator({
|
|||||||
templateId,
|
templateId,
|
||||||
publishedAt,
|
publishedAt,
|
||||||
importId,
|
importId,
|
||||||
title: templateDocument ? templateDocument.title : title,
|
title: templateDocument
|
||||||
|
? DocumentHelper.replaceTemplateVariables(templateDocument.title, user)
|
||||||
|
: title,
|
||||||
text: templateDocument ? templateDocument.text : text,
|
text: templateDocument ? templateDocument.text : text,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import {
|
|||||||
yDocToProsemirrorJSON,
|
yDocToProsemirrorJSON,
|
||||||
} from "@getoutline/y-prosemirror";
|
} from "@getoutline/y-prosemirror";
|
||||||
import { JSDOM } from "jsdom";
|
import { JSDOM } from "jsdom";
|
||||||
import { escapeRegExp } from "lodash";
|
import { escapeRegExp, startCase } from "lodash";
|
||||||
import { Node, DOMSerializer } from "prosemirror-model";
|
import { Node, DOMSerializer } from "prosemirror-model";
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { renderToString } from "react-dom/server";
|
import { renderToString } from "react-dom/server";
|
||||||
@@ -12,12 +12,19 @@ import * as Y from "yjs";
|
|||||||
import EditorContainer from "@shared/editor/components/Styles";
|
import EditorContainer from "@shared/editor/components/Styles";
|
||||||
import GlobalStyles from "@shared/styles/globals";
|
import GlobalStyles from "@shared/styles/globals";
|
||||||
import light from "@shared/styles/theme";
|
import light from "@shared/styles/theme";
|
||||||
|
import {
|
||||||
|
getCurrentDateAsString,
|
||||||
|
getCurrentDateTimeAsString,
|
||||||
|
getCurrentTimeAsString,
|
||||||
|
unicodeCLDRtoBCP47,
|
||||||
|
} from "@shared/utils/date";
|
||||||
import { isRTL } from "@shared/utils/rtl";
|
import { isRTL } from "@shared/utils/rtl";
|
||||||
import unescape from "@shared/utils/unescape";
|
import unescape from "@shared/utils/unescape";
|
||||||
import { parser, schema } from "@server/editor";
|
import { parser, schema } from "@server/editor";
|
||||||
import Logger from "@server/logging/Logger";
|
import Logger from "@server/logging/Logger";
|
||||||
import Document from "@server/models/Document";
|
import Document from "@server/models/Document";
|
||||||
import type Revision from "@server/models/Revision";
|
import type Revision from "@server/models/Revision";
|
||||||
|
import User from "@server/models/User";
|
||||||
import diff from "@server/utils/diff";
|
import diff from "@server/utils/diff";
|
||||||
import parseAttachmentIds from "@server/utils/parseAttachmentIds";
|
import parseAttachmentIds from "@server/utils/parseAttachmentIds";
|
||||||
import { getSignedUrl } from "@server/utils/s3";
|
import { getSignedUrl } from "@server/utils/s3";
|
||||||
@@ -307,6 +314,24 @@ export default class DocumentHelper {
|
|||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces template variables in the given text with the current date and time.
|
||||||
|
*
|
||||||
|
* @param text The text to replace the variables in
|
||||||
|
* @param user The user to get the language/locale from
|
||||||
|
* @returns The text with the variables replaced
|
||||||
|
*/
|
||||||
|
static replaceTemplateVariables(text: string, user: User) {
|
||||||
|
const locales = user.language
|
||||||
|
? unicodeCLDRtoBCP47(user.language)
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
return text
|
||||||
|
.replace("{date}", startCase(getCurrentDateAsString(locales)))
|
||||||
|
.replace("{time}", startCase(getCurrentTimeAsString(locales)))
|
||||||
|
.replace("{datetime}", startCase(getCurrentDateTimeAsString(locales)));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Applies the given Markdown to the document, this essentially creates a
|
* Applies the given Markdown to the document, this essentially creates a
|
||||||
* single change in the collaborative state that makes all the edits to get
|
* single change in the collaborative state that makes all the edits to get
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import i18n from "i18next";
|
import i18n from "i18next";
|
||||||
import backend from "i18next-http-backend";
|
import backend from "i18next-http-backend";
|
||||||
import { initReactI18next } from "react-i18next";
|
import { initReactI18next } from "react-i18next";
|
||||||
|
import { unicodeBCP47toCLDR, unicodeCLDRtoBCP47 } from "../utils/date";
|
||||||
|
|
||||||
// Note: Updating the available languages? Make sure to also update the
|
// Note: Updating the available languages? Make sure to also update the
|
||||||
// locales array in app/utils/i18n.js to enable translation for timestamps.
|
// locales array in app/utils/i18n.js to enable translation for timestamps.
|
||||||
@@ -77,14 +78,8 @@ export const languageOptions = [
|
|||||||
|
|
||||||
export const languages: string[] = languageOptions.map((i) => i.value);
|
export const languages: string[] = languageOptions.map((i) => i.value);
|
||||||
|
|
||||||
// Languages are stored in en_US format in the database, however the
|
|
||||||
// frontend translation framework (i18next) expects en-US
|
|
||||||
const underscoreToDash = (text: string) => text.replace("_", "-");
|
|
||||||
|
|
||||||
const dashToUnderscore = (text: string) => text.replace("-", "_");
|
|
||||||
|
|
||||||
export const initI18n = (defaultLanguage = "en_US") => {
|
export const initI18n = (defaultLanguage = "en_US") => {
|
||||||
const lng = underscoreToDash(defaultLanguage);
|
const lng = unicodeCLDRtoBCP47(defaultLanguage);
|
||||||
i18n
|
i18n
|
||||||
.use(backend)
|
.use(backend)
|
||||||
.use(initReactI18next)
|
.use(initReactI18next)
|
||||||
@@ -94,7 +89,7 @@ export const initI18n = (defaultLanguage = "en_US") => {
|
|||||||
// this must match the path defined in routes. It's the path that the
|
// this must match the path defined in routes. It's the path that the
|
||||||
// frontend UI code will hit to load missing translations.
|
// frontend UI code will hit to load missing translations.
|
||||||
loadPath: (languages: string[]) =>
|
loadPath: (languages: string[]) =>
|
||||||
`/locales/${dashToUnderscore(languages[0])}.json`,
|
`/locales/${unicodeBCP47toCLDR(languages[0])}.json`,
|
||||||
},
|
},
|
||||||
interpolation: {
|
interpolation: {
|
||||||
escapeValue: false,
|
escapeValue: false,
|
||||||
@@ -104,7 +99,7 @@ export const initI18n = (defaultLanguage = "en_US") => {
|
|||||||
},
|
},
|
||||||
lng,
|
lng,
|
||||||
fallbackLng: lng,
|
fallbackLng: lng,
|
||||||
supportedLngs: languages.map(underscoreToDash),
|
supportedLngs: languages.map(unicodeCLDRtoBCP47),
|
||||||
// Uncomment when debugging translation framework, otherwise it's noisy
|
// Uncomment when debugging translation framework, otherwise it's noisy
|
||||||
keySeparator: false,
|
keySeparator: false,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -20,13 +20,33 @@ export function subtractDate(date: Date, period: DateFilter) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a locale string from Unicode CLDR format to BCP47 format.
|
||||||
|
*
|
||||||
|
* @param locale The locale string to convert
|
||||||
|
* @returns The converted locale string
|
||||||
|
*/
|
||||||
|
export function unicodeCLDRtoBCP47(locale: string) {
|
||||||
|
return locale.replace("_", "-").replace("root", "und");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a locale string from BCP47 format to Unicode CLDR format.
|
||||||
|
*
|
||||||
|
* @param locale The locale string to convert
|
||||||
|
* @returns The converted locale string
|
||||||
|
*/
|
||||||
|
export function unicodeBCP47toCLDR(locale: string) {
|
||||||
|
return locale.replace("-", "_").replace("und", "root");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current date as a string formatted depending on current locale.
|
* Returns the current date as a string formatted depending on current locale.
|
||||||
*
|
*
|
||||||
* @returns The current date
|
* @returns The current date
|
||||||
*/
|
*/
|
||||||
export function getCurrentDateAsString() {
|
export function getCurrentDateAsString(locales?: Intl.LocalesArgument) {
|
||||||
return new Date().toLocaleDateString(undefined, {
|
return new Date().toLocaleDateString(locales, {
|
||||||
year: "numeric",
|
year: "numeric",
|
||||||
month: "long",
|
month: "long",
|
||||||
day: "numeric",
|
day: "numeric",
|
||||||
@@ -38,8 +58,8 @@ export function getCurrentDateAsString() {
|
|||||||
*
|
*
|
||||||
* @returns The current time
|
* @returns The current time
|
||||||
*/
|
*/
|
||||||
export function getCurrentTimeAsString() {
|
export function getCurrentTimeAsString(locales?: Intl.LocalesArgument) {
|
||||||
return new Date().toLocaleTimeString(undefined, {
|
return new Date().toLocaleTimeString(locales, {
|
||||||
hour: "numeric",
|
hour: "numeric",
|
||||||
minute: "numeric",
|
minute: "numeric",
|
||||||
});
|
});
|
||||||
@@ -51,8 +71,8 @@ export function getCurrentTimeAsString() {
|
|||||||
*
|
*
|
||||||
* @returns The current date and time
|
* @returns The current date and time
|
||||||
*/
|
*/
|
||||||
export function getCurrentDateTimeAsString() {
|
export function getCurrentDateTimeAsString(locales?: Intl.LocalesArgument) {
|
||||||
return new Date().toLocaleString(undefined, {
|
return new Date().toLocaleString(locales, {
|
||||||
year: "numeric",
|
year: "numeric",
|
||||||
month: "long",
|
month: "long",
|
||||||
day: "numeric",
|
day: "numeric",
|
||||||
|
|||||||
Reference in New Issue
Block a user