2020-01-22 20:42:24 +02:00
|
|
|
import { attempt, flatten, isError, uniq } from 'lodash';
|
|
|
|
import { List, Map, fromJS } from 'immutable';
|
2019-12-18 18:16:02 +02:00
|
|
|
import * as fuzzy from 'fuzzy';
|
|
|
|
import { resolveFormat } from './formats/formats';
|
2020-01-15 00:15:14 +02:00
|
|
|
import { selectUseWorkflow } from './reducers/config';
|
2019-12-18 18:16:02 +02:00
|
|
|
import { selectMediaFilePath, selectMediaFolder } from './reducers/entries';
|
|
|
|
import { selectIntegration } from './reducers/integrations';
|
WIP - Global UI (#785)
* update top bar and collections sidebar UI
* update collection entries UI
* improve global layout
* merge search page into collection page
* enable new entry button
* search fixup
* wip -initial editor update
* update editor scrolling and markdown toolbar position
* wip
* editor toolbar progress
* editor toolbar wip
* finished basic editor toolbar
* add standalone toggle component
* improve markdown toolbar spacing
* add user avatar placeholder
* finish markdown toggle styling
* refactor icon setup, add new icons
* add new icons to markdown editor toolbar
* remove extra app container
* add markdown active mark style
* relation and text widget styling
* widget design updates, basic list/object design update
* widget style updates, image widget improvements
* refactor widget directory, fix file removal
* widget focus styles
* finish editor widget focus styles
* migrate media library modal to react-modal
* wip - migrate editor component form to modal
* wip - move editor component form to modal
* wip - embed plugin forms in the editor
* inline shortcode forms working
* disable react hot loading, its breaking things
* improve shortcode form styles
* make shortcode form collapsible, improve styling
* add close functionality to shortcode blocks
* improve base media library styling
* fix shortcode label
* migrate unstyled workflow to new UI
* wip - reorganizing everything
* more work moving everything
* finish more moving and eliminating stuff
* restructure, remove react-toolbox
* wip - removing old stuff, more restructure
* finish restructure
* wip - css arch
* switch back to test repo
* update react-datetime to ^2.11.0
* remove leftover react-toolbox button
* more restructuring clean-up
* fix UI component directory case
* wip -css editor control style
* wip - consolidate widget styles
* wip - use a single control renderer
* fixed object values breaking
* wip - editor control active styles
* pass control wrapper to widgets
* ensure branch name is trimmed
* wip - improve widget authoring support
* import Map to Widget component
* refactor toolbar buttons
* wip - more widget active styles
* break out editor toggle component
* add local scroll sync back
* update editor toggle icons
* limit editor control pane content width
* fix editor control spacing
* migrate markdown toolbar stickiness to css
* fix markdown toolbar border radius
* temporarily use test backend
* stop markdown toolbar from going to bottom
* restore disabled markdown toolbar buttons for raw
* test markdown widget without focus styles
* more widget updates
* remove card visuals from editor
* disable dragging editor split off screen
* use editorControl component for shortcode fields
* make header site link configurable
* add configurable collection descriptions
* temporarily add example assets
* add basic list view
* remove outdated css mixins
* add and implement search icon
* activate quick add menu
* visualize usable space in editor view
* fix entry close, other improvements
* wip - editorial workflow updates
* some dropshadow and other CSS tweaks
* workflow ui updates
* add worfklow card buttons
* fix workflow card button handlers
* some dropshadow and other CSS tweaks
* make workflow board wider
* center workflow and collection views
* add basic responsiveness
* a bunch of fun UI fixes! a BUNCH! (#875)
* give `.nc-entryEditor-toolbar-mainSection` left and right child divs
* a bunch of fun UI fixes! a BUNCH!
* remove obscure --buttonShadow
* revert to test repo
* fix not found page styling
* allow workflow publishing from any column
* disallow publishing from all columns, with feedback
* fix new entry button
* fix markdown state persisting across entries
* enable simple workflow save and new from editor
* update slug in address bar when saving new entry
* wip - workflow updates, deletion working
* add status change functionality to editor
* wip - improving status change from editor
* editor toolbar back button improvements, loading improvements, cleanup
* progress on the media library UI cleanup
* remove font smothing css
* a quick fix for these buttons
* tweaks
* progress on media library modal— broken FYI
* fix media library functionality, finish migrating footer
* remove media library footer files
* remove leftover css import
* fix media library
* editor publishing functionality complete (unstyled)
* remove leftover loader var from media library
* wip - editor publishing styles
* add status dropdown styling
* editor toolbar style updates
* editor toolbar state improvements
* progress on the media library UI cleanup, style improvements
* finish editorial workflow editor styling
* finish media library styling
* fix config
* add what-input to optimize focus styling
* fix button
* fix navigation blocking for simple workflow
* improve simple workflow publishing
* add avatar dropdown to editor top bar
* style github and test-repo auth pages
* add git gateway auth page styles
* improve editor error styling
2017-12-07 12:37:10 -05:00
|
|
|
import {
|
|
|
|
selectEntrySlug,
|
|
|
|
selectEntryPath,
|
2019-11-08 23:28:17 +00:00
|
|
|
selectFileEntryLabel,
|
WIP - Global UI (#785)
* update top bar and collections sidebar UI
* update collection entries UI
* improve global layout
* merge search page into collection page
* enable new entry button
* search fixup
* wip -initial editor update
* update editor scrolling and markdown toolbar position
* wip
* editor toolbar progress
* editor toolbar wip
* finished basic editor toolbar
* add standalone toggle component
* improve markdown toolbar spacing
* add user avatar placeholder
* finish markdown toggle styling
* refactor icon setup, add new icons
* add new icons to markdown editor toolbar
* remove extra app container
* add markdown active mark style
* relation and text widget styling
* widget design updates, basic list/object design update
* widget style updates, image widget improvements
* refactor widget directory, fix file removal
* widget focus styles
* finish editor widget focus styles
* migrate media library modal to react-modal
* wip - migrate editor component form to modal
* wip - move editor component form to modal
* wip - embed plugin forms in the editor
* inline shortcode forms working
* disable react hot loading, its breaking things
* improve shortcode form styles
* make shortcode form collapsible, improve styling
* add close functionality to shortcode blocks
* improve base media library styling
* fix shortcode label
* migrate unstyled workflow to new UI
* wip - reorganizing everything
* more work moving everything
* finish more moving and eliminating stuff
* restructure, remove react-toolbox
* wip - removing old stuff, more restructure
* finish restructure
* wip - css arch
* switch back to test repo
* update react-datetime to ^2.11.0
* remove leftover react-toolbox button
* more restructuring clean-up
* fix UI component directory case
* wip -css editor control style
* wip - consolidate widget styles
* wip - use a single control renderer
* fixed object values breaking
* wip - editor control active styles
* pass control wrapper to widgets
* ensure branch name is trimmed
* wip - improve widget authoring support
* import Map to Widget component
* refactor toolbar buttons
* wip - more widget active styles
* break out editor toggle component
* add local scroll sync back
* update editor toggle icons
* limit editor control pane content width
* fix editor control spacing
* migrate markdown toolbar stickiness to css
* fix markdown toolbar border radius
* temporarily use test backend
* stop markdown toolbar from going to bottom
* restore disabled markdown toolbar buttons for raw
* test markdown widget without focus styles
* more widget updates
* remove card visuals from editor
* disable dragging editor split off screen
* use editorControl component for shortcode fields
* make header site link configurable
* add configurable collection descriptions
* temporarily add example assets
* add basic list view
* remove outdated css mixins
* add and implement search icon
* activate quick add menu
* visualize usable space in editor view
* fix entry close, other improvements
* wip - editorial workflow updates
* some dropshadow and other CSS tweaks
* workflow ui updates
* add worfklow card buttons
* fix workflow card button handlers
* some dropshadow and other CSS tweaks
* make workflow board wider
* center workflow and collection views
* add basic responsiveness
* a bunch of fun UI fixes! a BUNCH! (#875)
* give `.nc-entryEditor-toolbar-mainSection` left and right child divs
* a bunch of fun UI fixes! a BUNCH!
* remove obscure --buttonShadow
* revert to test repo
* fix not found page styling
* allow workflow publishing from any column
* disallow publishing from all columns, with feedback
* fix new entry button
* fix markdown state persisting across entries
* enable simple workflow save and new from editor
* update slug in address bar when saving new entry
* wip - workflow updates, deletion working
* add status change functionality to editor
* wip - improving status change from editor
* editor toolbar back button improvements, loading improvements, cleanup
* progress on the media library UI cleanup
* remove font smothing css
* a quick fix for these buttons
* tweaks
* progress on media library modal— broken FYI
* fix media library functionality, finish migrating footer
* remove media library footer files
* remove leftover css import
* fix media library
* editor publishing functionality complete (unstyled)
* remove leftover loader var from media library
* wip - editor publishing styles
* add status dropdown styling
* editor toolbar style updates
* editor toolbar state improvements
* progress on the media library UI cleanup, style improvements
* finish editorial workflow editor styling
* finish media library styling
* fix config
* add what-input to optimize focus styling
* fix button
* fix navigation blocking for simple workflow
* improve simple workflow publishing
* add avatar dropdown to editor top bar
* style github and test-repo auth pages
* add git gateway auth page styles
* improve editor error styling
2017-12-07 12:37:10 -05:00
|
|
|
selectAllowNewEntries,
|
|
|
|
selectAllowDeletion,
|
2018-05-25 10:15:01 -06:00
|
|
|
selectFolderEntryExtension,
|
2018-06-11 19:03:43 -07:00
|
|
|
selectInferedField,
|
2019-12-18 18:16:02 +02:00
|
|
|
} from './reducers/collections';
|
|
|
|
import { createEntry, EntryValue } from './valueObjects/Entry';
|
2020-01-22 20:42:24 +02:00
|
|
|
import { sanitizeChar } from './lib/urlHelper';
|
2020-01-31 16:44:01 -08:00
|
|
|
import { getBackend, invokeEvent } from './lib/registry';
|
2020-01-22 20:42:24 +02:00
|
|
|
import { commitMessageFormatter, slugFormatter, previewUrlFormatter } from './lib/formatters';
|
2019-04-10 21:38:53 +01:00
|
|
|
import {
|
|
|
|
localForage,
|
|
|
|
Cursor,
|
|
|
|
CURSOR_COMPATIBILITY_SYMBOL,
|
|
|
|
EditorialWorkflowError,
|
2020-01-15 00:15:14 +02:00
|
|
|
Implementation as BackendImplementation,
|
|
|
|
DisplayURL,
|
|
|
|
ImplementationEntry,
|
|
|
|
ImplementationMediaFile,
|
|
|
|
Credentials,
|
|
|
|
User,
|
|
|
|
getPathDepth,
|
|
|
|
Config as ImplementationConfig,
|
2019-04-10 21:38:53 +01:00
|
|
|
} from 'netlify-cms-lib-util';
|
2020-01-15 00:15:14 +02:00
|
|
|
import { status } from './constants/publishModes';
|
2020-01-22 20:42:24 +02:00
|
|
|
import { extractTemplateVars, dateParsers } from './lib/stringTemplate';
|
2019-12-18 18:16:02 +02:00
|
|
|
import {
|
|
|
|
Collection,
|
|
|
|
EntryMap,
|
|
|
|
Config,
|
|
|
|
FilterRule,
|
|
|
|
Collections,
|
|
|
|
EntryDraft,
|
|
|
|
CollectionFile,
|
|
|
|
State,
|
|
|
|
} from './types/redux';
|
|
|
|
import AssetProxy from './valueObjects/AssetProxy';
|
2020-01-15 00:15:14 +02:00
|
|
|
import { FOLDER, FILES } from './constants/collectionTypes';
|
2018-01-25 08:55:13 -08:00
|
|
|
|
2019-06-26 11:51:35 -04:00
|
|
|
export class LocalStorageAuthStore {
|
2018-08-07 14:46:54 -06:00
|
|
|
storageKey = 'netlify-cms-user';
|
2016-05-30 16:55:32 -07:00
|
|
|
|
|
|
|
retrieve() {
|
|
|
|
const data = window.localStorage.getItem(this.storageKey);
|
|
|
|
return data && JSON.parse(data);
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
store(userData: unknown) {
|
2016-05-30 16:55:32 -07:00
|
|
|
window.localStorage.setItem(this.storageKey, JSON.stringify(userData));
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
2016-11-01 14:35:20 +01:00
|
|
|
|
|
|
|
logout() {
|
|
|
|
window.localStorage.removeItem(this.storageKey);
|
|
|
|
}
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
function getEntryBackupKey(collectionName?: string, slug?: string) {
|
2019-02-28 13:30:23 -05:00
|
|
|
const baseKey = 'backup';
|
|
|
|
if (!collectionName) {
|
|
|
|
return baseKey;
|
|
|
|
}
|
|
|
|
const suffix = slug ? `.${slug}` : '';
|
2019-03-29 18:30:38 +00:00
|
|
|
return `${baseKey}.${collectionName}${suffix}`;
|
2019-02-28 13:30:23 -05:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
const extractSearchFields = (searchFields: string[]) => (entry: EntryValue) =>
|
2018-08-07 14:46:54 -06:00
|
|
|
searchFields.reduce((acc, field) => {
|
2019-12-18 18:16:02 +02:00
|
|
|
const nestedFields = field.split('.');
|
2019-06-24 17:32:02 -06:00
|
|
|
let f = entry.data;
|
|
|
|
for (let i = 0; i < nestedFields.length; i++) {
|
|
|
|
f = f[nestedFields[i]];
|
|
|
|
if (!f) break;
|
|
|
|
}
|
2018-08-07 14:46:54 -06:00
|
|
|
return f ? `${acc} ${f}` : acc;
|
|
|
|
}, '');
|
2018-06-11 19:03:43 -07:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
const sortByScore = (a: fuzzy.FilterResult<EntryValue>, b: fuzzy.FilterResult<EntryValue>) => {
|
2018-06-11 19:03:43 -07:00
|
|
|
if (a.score > b.score) return -1;
|
|
|
|
if (a.score < b.score) return 1;
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
interface AuthStore {
|
|
|
|
retrieve: () => User;
|
|
|
|
store: (user: User) => void;
|
|
|
|
logout: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface BackendOptions {
|
|
|
|
backendName?: string;
|
|
|
|
authStore?: AuthStore | null;
|
|
|
|
config?: Config;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface BackupEntry {
|
|
|
|
raw: string;
|
|
|
|
path: string;
|
2020-01-15 00:15:14 +02:00
|
|
|
mediaFiles: ImplementationMediaFile[];
|
2019-12-18 18:16:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface PersistArgs {
|
|
|
|
config: Config;
|
|
|
|
collection: Collection;
|
|
|
|
entryDraft: EntryDraft;
|
|
|
|
assetProxies: AssetProxy[];
|
|
|
|
usedSlugs: List<string>;
|
|
|
|
unpublished?: boolean;
|
|
|
|
status?: string;
|
|
|
|
}
|
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
interface ImplementationInitOptions {
|
|
|
|
useWorkflow: boolean;
|
|
|
|
updateUserCredentials: (credentials: Credentials) => void;
|
|
|
|
initialWorkflowStatus: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Implementation = BackendImplementation & {
|
|
|
|
init: (config: ImplementationConfig, options: ImplementationInitOptions) => Implementation;
|
|
|
|
};
|
|
|
|
|
2019-06-26 11:51:35 -04:00
|
|
|
export class Backend {
|
2019-12-18 18:16:02 +02:00
|
|
|
implementation: Implementation;
|
|
|
|
backendName: string;
|
|
|
|
authStore: AuthStore | null;
|
|
|
|
config: Config;
|
|
|
|
user?: User | null;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
implementation: Implementation,
|
|
|
|
{ backendName, authStore = null, config }: BackendOptions = {},
|
|
|
|
) {
|
2019-02-28 13:30:23 -05:00
|
|
|
// We can't reliably run this on exit, so we do cleanup on load.
|
|
|
|
this.deleteAnonymousBackup();
|
2019-12-18 18:16:02 +02:00
|
|
|
this.config = config as Config;
|
2020-01-15 00:15:14 +02:00
|
|
|
this.implementation = implementation.init(this.config.toJS(), {
|
|
|
|
useWorkflow: selectUseWorkflow(this.config),
|
2018-07-23 12:14:53 -04:00
|
|
|
updateUserCredentials: this.updateUserCredentials,
|
2018-07-31 12:17:25 -04:00
|
|
|
initialWorkflowStatus: status.first(),
|
2018-07-23 12:14:53 -04:00
|
|
|
});
|
2019-12-18 18:16:02 +02:00
|
|
|
this.backendName = backendName as string;
|
2016-02-25 12:31:21 -08:00
|
|
|
this.authStore = authStore;
|
2016-09-05 12:12:38 -03:00
|
|
|
if (this.implementation === null) {
|
2018-08-07 14:46:54 -06:00
|
|
|
throw new Error('Cannot instantiate a Backend with no implementation');
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
currentUser() {
|
2018-08-07 14:46:54 -06:00
|
|
|
if (this.user) {
|
|
|
|
return this.user;
|
|
|
|
}
|
2020-01-15 00:15:14 +02:00
|
|
|
const stored = this.authStore!.retrieve();
|
2017-08-29 13:08:15 -06:00
|
|
|
if (stored && stored.backendName === this.backendName) {
|
2018-08-07 14:46:54 -06:00
|
|
|
return Promise.resolve(this.implementation.restoreUser(stored)).then(user => {
|
2019-06-26 11:51:35 -04:00
|
|
|
this.user = { ...user, backendName: this.backendName };
|
2017-08-29 13:08:15 -06:00
|
|
|
// return confirmed/rehydrated user object instead of stored
|
2020-01-15 00:15:14 +02:00
|
|
|
this.authStore!.store(this.user as User);
|
2019-06-26 11:51:35 -04:00
|
|
|
return this.user;
|
2017-08-27 20:12:54 -06:00
|
|
|
});
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
2016-12-23 16:59:48 -02:00
|
|
|
return Promise.resolve(null);
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
updateUserCredentials = (updatedCredentials: Credentials) => {
|
2020-01-15 00:15:14 +02:00
|
|
|
const storedUser = this.authStore!.retrieve();
|
2018-07-19 18:11:23 -07:00
|
|
|
if (storedUser && storedUser.backendName === this.backendName) {
|
2019-06-26 11:51:35 -04:00
|
|
|
this.user = { ...storedUser, ...updatedCredentials };
|
2020-01-15 00:15:14 +02:00
|
|
|
this.authStore!.store(this.user as User);
|
2019-06-26 11:51:35 -04:00
|
|
|
return this.user;
|
2018-07-19 18:11:23 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-25 12:31:21 -08:00
|
|
|
authComponent() {
|
|
|
|
return this.implementation.authComponent();
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
authenticate(credentials: Credentials) {
|
2018-08-07 14:46:54 -06:00
|
|
|
return this.implementation.authenticate(credentials).then(user => {
|
2019-06-26 11:51:35 -04:00
|
|
|
this.user = { ...user, backendName: this.backendName };
|
2018-08-07 14:46:54 -06:00
|
|
|
if (this.authStore) {
|
2019-12-18 18:16:02 +02:00
|
|
|
this.authStore.store(this.user as User);
|
2018-08-07 14:46:54 -06:00
|
|
|
}
|
2019-06-26 11:51:35 -04:00
|
|
|
return this.user;
|
2016-05-30 16:55:32 -07:00
|
|
|
});
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
|
2016-11-01 14:35:20 +01:00
|
|
|
logout() {
|
2017-09-05 19:30:03 -07:00
|
|
|
return Promise.resolve(this.implementation.logout()).then(() => {
|
2019-06-26 11:51:35 -04:00
|
|
|
this.user = null;
|
2017-09-05 19:30:03 -07:00
|
|
|
if (this.authStore) {
|
|
|
|
this.authStore.logout();
|
|
|
|
}
|
|
|
|
});
|
2016-11-01 14:35:20 +01:00
|
|
|
}
|
|
|
|
|
2017-01-10 22:23:22 -02:00
|
|
|
getToken = () => this.implementation.getToken();
|
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
async entryExist(collection: Collection, path: string, slug: string, useWorkflow: boolean) {
|
2019-04-10 21:38:53 +01:00
|
|
|
const unpublishedEntry =
|
2020-01-15 00:15:14 +02:00
|
|
|
useWorkflow &&
|
|
|
|
(await this.implementation.unpublishedEntry(collection.get('name'), slug).catch(error => {
|
2019-04-10 21:38:53 +01:00
|
|
|
if (error instanceof EditorialWorkflowError && error.notUnderEditorialWorkflow) {
|
|
|
|
return Promise.resolve(false);
|
|
|
|
}
|
|
|
|
return Promise.reject(error);
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (unpublishedEntry) return unpublishedEntry;
|
|
|
|
|
|
|
|
const publishedEntry = await this.implementation
|
2020-01-15 00:15:14 +02:00
|
|
|
.getEntry(path)
|
2019-04-10 21:38:53 +01:00
|
|
|
.then(({ data }) => data)
|
2019-06-26 19:37:22 +01:00
|
|
|
.catch(() => {
|
|
|
|
return Promise.resolve(false);
|
2019-04-10 21:38:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return publishedEntry;
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
async generateUniqueSlug(
|
|
|
|
collection: Collection,
|
2020-01-22 20:42:24 +02:00
|
|
|
entryData: Map<string, unknown>,
|
2020-01-15 00:15:14 +02:00
|
|
|
config: Config,
|
2019-12-18 18:16:02 +02:00
|
|
|
usedSlugs: List<string>,
|
|
|
|
) {
|
2020-01-15 00:15:14 +02:00
|
|
|
const slugConfig = config.get('slug');
|
2019-12-18 18:16:02 +02:00
|
|
|
const slug: string = slugFormatter(collection, entryData, slugConfig);
|
2019-04-10 21:38:53 +01:00
|
|
|
let i = 1;
|
2019-11-28 05:39:33 +02:00
|
|
|
let uniqueSlug = slug;
|
2019-04-10 21:38:53 +01:00
|
|
|
|
|
|
|
// Check for duplicate slug in loaded entities store first before repo
|
|
|
|
while (
|
|
|
|
usedSlugs.includes(uniqueSlug) ||
|
2019-12-18 18:16:02 +02:00
|
|
|
(await this.entryExist(
|
|
|
|
collection,
|
|
|
|
selectEntryPath(collection, uniqueSlug) as string,
|
|
|
|
uniqueSlug,
|
2020-01-15 00:15:14 +02:00
|
|
|
selectUseWorkflow(config),
|
2019-12-18 18:16:02 +02:00
|
|
|
))
|
2019-04-10 21:38:53 +01:00
|
|
|
) {
|
2019-11-28 05:39:33 +02:00
|
|
|
uniqueSlug = `${slug}${sanitizeChar(' ', slugConfig)}${i++}`;
|
2019-04-10 21:38:53 +01:00
|
|
|
}
|
|
|
|
return uniqueSlug;
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
processEntries(loadedEntries: ImplementationEntry[], collection: Collection) {
|
2018-06-11 19:03:43 -07:00
|
|
|
const collectionFilter = collection.get('filter');
|
2018-08-07 14:46:54 -06:00
|
|
|
const entries = loadedEntries.map(loadedEntry =>
|
|
|
|
createEntry(
|
|
|
|
collection.get('name'),
|
|
|
|
selectEntrySlug(collection, loadedEntry.file.path),
|
|
|
|
loadedEntry.file.path,
|
|
|
|
{ raw: loadedEntry.data || '', label: loadedEntry.file.label },
|
|
|
|
),
|
|
|
|
);
|
2018-06-11 19:03:43 -07:00
|
|
|
const formattedEntries = entries.map(this.entryWithFormat(collection));
|
|
|
|
// If this collection has a "filter" property, filter entries accordingly
|
|
|
|
const filteredEntries = collectionFilter
|
|
|
|
? this.filterEntries({ entries: formattedEntries }, collectionFilter)
|
|
|
|
: formattedEntries;
|
|
|
|
return filteredEntries;
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
listEntries(collection: Collection) {
|
2017-04-14 19:19:45 +01:00
|
|
|
const extension = selectFolderEntryExtension(collection);
|
2020-01-15 00:15:14 +02:00
|
|
|
let listMethod: () => Promise<ImplementationEntry[]>;
|
|
|
|
const collectionType = collection.get('type');
|
|
|
|
if (collectionType === FOLDER) {
|
|
|
|
listMethod = () =>
|
|
|
|
this.implementation.entriesByFolder(
|
|
|
|
collection.get('folder') as string,
|
|
|
|
extension,
|
|
|
|
getPathDepth(collection.get('path', '') as string),
|
|
|
|
);
|
|
|
|
} else if (collectionType === FILES) {
|
|
|
|
const files = collection
|
|
|
|
.get('files')!
|
|
|
|
.map(collectionFile => ({
|
|
|
|
path: collectionFile!.get('file'),
|
|
|
|
label: collectionFile!.get('label'),
|
|
|
|
}))
|
|
|
|
.toArray();
|
|
|
|
listMethod = () => this.implementation.entriesByFiles(files);
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unknown collection type: ${collectionType}`);
|
|
|
|
}
|
|
|
|
return listMethod().then((loadedEntries: ImplementationEntry[]) => ({
|
|
|
|
entries: this.processEntries(loadedEntries, collection),
|
|
|
|
/*
|
2018-06-11 19:03:43 -07:00
|
|
|
Wrap cursors so we can tell which collection the cursor is
|
|
|
|
from. This is done to prevent traverseCursor from requiring a
|
|
|
|
`collection` argument.
|
|
|
|
*/
|
2020-01-15 00:15:14 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
|
|
|
|
// @ts-ignore
|
|
|
|
cursor: Cursor.create(loadedEntries[CURSOR_COMPATIBILITY_SYMBOL]).wrapData({
|
|
|
|
cursorType: 'collectionEntries',
|
|
|
|
collection,
|
|
|
|
}),
|
|
|
|
}));
|
2018-06-11 19:03:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The same as listEntries, except that if a cursor with the "next"
|
|
|
|
// action available is returned, it calls "next" on the cursor and
|
|
|
|
// repeats the process. Once there is no available "next" action, it
|
|
|
|
// returns all the collected entries. Used to retrieve all entries
|
|
|
|
// for local searches and queries.
|
2019-12-18 18:16:02 +02:00
|
|
|
async listAllEntries(collection: Collection) {
|
2018-08-07 14:46:54 -06:00
|
|
|
if (collection.get('folder') && this.implementation.allEntriesByFolder) {
|
2018-06-11 19:03:43 -07:00
|
|
|
const extension = selectFolderEntryExtension(collection);
|
2018-08-07 14:46:54 -06:00
|
|
|
return this.implementation
|
2020-01-15 00:15:14 +02:00
|
|
|
.allEntriesByFolder(
|
|
|
|
collection.get('folder') as string,
|
|
|
|
extension,
|
|
|
|
getPathDepth(collection.get('path', '') as string),
|
|
|
|
)
|
2018-08-07 14:46:54 -06:00
|
|
|
.then(entries => this.processEntries(entries, collection));
|
2018-06-11 19:03:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const response = await this.listEntries(collection);
|
|
|
|
const { entries } = response;
|
|
|
|
let { cursor } = response;
|
2020-01-15 00:15:14 +02:00
|
|
|
while (cursor && cursor.actions!.includes('next')) {
|
2018-08-07 14:46:54 -06:00
|
|
|
const { entries: newEntries, cursor: newCursor } = await this.traverseCursor(cursor, 'next');
|
2018-06-11 19:03:43 -07:00
|
|
|
entries.push(...newEntries);
|
|
|
|
cursor = newCursor;
|
|
|
|
}
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
async search(collections: Collection[], searchTerm: string) {
|
2018-06-11 19:03:43 -07:00
|
|
|
// Perform a local search by requesting all entries. For each
|
|
|
|
// collection, load it, search, and call onCollectionResults with
|
|
|
|
// its results.
|
2019-12-18 18:16:02 +02:00
|
|
|
const errors: Error[] = [];
|
2018-08-07 14:46:54 -06:00
|
|
|
const collectionEntriesRequests = collections
|
|
|
|
.map(async collection => {
|
2019-12-18 18:16:02 +02:00
|
|
|
const summary = collection.get('summary', '') as string;
|
2019-03-29 18:30:38 +00:00
|
|
|
const summaryFields = extractTemplateVars(summary);
|
|
|
|
|
2018-08-07 14:46:54 -06:00
|
|
|
// TODO: pass search fields in as an argument
|
|
|
|
const searchFields = [
|
|
|
|
selectInferedField(collection, 'title'),
|
|
|
|
selectInferedField(collection, 'shortTitle'),
|
|
|
|
selectInferedField(collection, 'author'),
|
2019-03-29 18:30:38 +00:00
|
|
|
...summaryFields.map(elem => {
|
|
|
|
if (dateParsers[elem]) {
|
|
|
|
return selectInferedField(collection, 'date');
|
|
|
|
}
|
|
|
|
return elem;
|
|
|
|
}),
|
2019-12-18 18:16:02 +02:00
|
|
|
].filter(Boolean) as string[];
|
2018-08-07 14:46:54 -06:00
|
|
|
const collectionEntries = await this.listAllEntries(collection);
|
|
|
|
return fuzzy.filter(searchTerm, collectionEntries, {
|
2019-03-29 18:30:38 +00:00
|
|
|
extract: extractSearchFields(uniq(searchFields)),
|
2018-08-07 14:46:54 -06:00
|
|
|
});
|
|
|
|
})
|
2019-12-18 18:16:02 +02:00
|
|
|
.map(p =>
|
|
|
|
p.catch(err => {
|
|
|
|
errors.push(err);
|
|
|
|
return [] as fuzzy.FilterResult<EntryValue>[];
|
|
|
|
}),
|
|
|
|
);
|
2018-06-11 19:03:43 -07:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
const entries = await Promise.all(collectionEntriesRequests).then(arrays => flatten(arrays));
|
2018-06-11 19:03:43 -07:00
|
|
|
|
|
|
|
if (errors.length > 0) {
|
2019-12-18 18:16:02 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
|
|
|
|
// @ts-ignore
|
2018-08-07 14:46:54 -06:00
|
|
|
throw new Error({ message: 'Errors ocurred while searching entries locally!', errors });
|
2018-06-11 19:03:43 -07:00
|
|
|
}
|
2019-12-18 18:16:02 +02:00
|
|
|
|
2018-08-07 14:46:54 -06:00
|
|
|
const hits = entries
|
2019-12-18 18:16:02 +02:00
|
|
|
.filter(({ score }: fuzzy.FilterResult<EntryValue>) => score > 5)
|
2018-08-07 14:46:54 -06:00
|
|
|
.sort(sortByScore)
|
2019-12-18 18:16:02 +02:00
|
|
|
.map((f: fuzzy.FilterResult<EntryValue>) => f.original);
|
2018-06-11 19:03:43 -07:00
|
|
|
return { entries: hits };
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
async query(collection: Collection, searchFields: string[], searchTerm: string) {
|
2018-06-11 19:03:43 -07:00
|
|
|
const entries = await this.listAllEntries(collection);
|
2018-08-07 14:46:54 -06:00
|
|
|
const hits = fuzzy
|
|
|
|
.filter(searchTerm, entries, { extract: extractSearchFields(searchFields) })
|
2018-06-11 19:03:43 -07:00
|
|
|
.sort(sortByScore)
|
|
|
|
.map(f => f.original);
|
|
|
|
return { query: searchTerm, hits };
|
|
|
|
}
|
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
traverseCursor(cursor: Cursor, action: string) {
|
2018-06-11 19:03:43 -07:00
|
|
|
const [data, unwrappedCursor] = cursor.unwrapData();
|
|
|
|
// TODO: stop assuming all cursors are for collections
|
2020-01-15 00:15:14 +02:00
|
|
|
const collection = data.get('collection') as Collection;
|
|
|
|
return this.implementation!.traverseCursor!(unwrappedCursor, action).then(
|
|
|
|
async ({ entries, cursor: newCursor }) => ({
|
2018-06-11 19:03:43 -07:00
|
|
|
entries: this.processEntries(entries, collection),
|
|
|
|
cursor: Cursor.create(newCursor).wrapData({
|
2018-08-07 14:46:54 -06:00
|
|
|
cursorType: 'collectionEntries',
|
2018-06-11 19:03:43 -07:00
|
|
|
collection,
|
|
|
|
}),
|
2020-01-15 00:15:14 +02:00
|
|
|
}),
|
|
|
|
);
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
async getLocalDraftBackup(collection: Collection, slug: string) {
|
2019-02-28 13:30:23 -05:00
|
|
|
const key = getEntryBackupKey(collection.get('name'), slug);
|
2019-12-18 18:16:02 +02:00
|
|
|
const backup = await localForage.getItem<BackupEntry>(key);
|
2019-02-28 13:30:23 -05:00
|
|
|
if (!backup || !backup.raw.trim()) {
|
2019-11-17 11:51:50 +02:00
|
|
|
return {};
|
2019-02-28 13:30:23 -05:00
|
|
|
}
|
2019-12-18 18:16:02 +02:00
|
|
|
const { raw, path } = backup;
|
|
|
|
let { mediaFiles = [] } = backup;
|
|
|
|
|
|
|
|
mediaFiles = mediaFiles.map(file => {
|
|
|
|
// de-serialize the file object
|
|
|
|
if (file.file) {
|
|
|
|
return { ...file, url: URL.createObjectURL(file.file) };
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
});
|
2019-11-17 11:51:50 +02:00
|
|
|
|
2019-11-08 23:28:17 +00:00
|
|
|
const label = selectFileEntryLabel(collection, slug);
|
2019-12-18 18:16:02 +02:00
|
|
|
const entry: EntryValue = this.entryWithFormat(collection)(
|
|
|
|
createEntry(collection.get('name'), slug, path, { raw, label, mediaFiles }),
|
|
|
|
);
|
2019-11-17 11:51:50 +02:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
return { entry };
|
2019-02-28 13:30:23 -05:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
async persistLocalDraftBackup(entry: EntryMap, collection: Collection) {
|
2019-02-28 13:30:23 -05:00
|
|
|
const key = getEntryBackupKey(collection.get('name'), entry.get('slug'));
|
|
|
|
const raw = this.entryToRaw(collection, entry);
|
|
|
|
if (!raw.trim()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-17 11:51:50 +02:00
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
const mediaFiles = await Promise.all<ImplementationMediaFile>(
|
2019-12-18 18:16:02 +02:00
|
|
|
entry
|
|
|
|
.get('mediaFiles')
|
|
|
|
.toJS()
|
2020-01-15 00:15:14 +02:00
|
|
|
.map(async (file: ImplementationMediaFile) => {
|
2019-12-18 18:16:02 +02:00
|
|
|
// make sure to serialize the file
|
|
|
|
if (file.url?.startsWith('blob:')) {
|
2020-01-15 00:15:14 +02:00
|
|
|
const blob = await fetch(file.url as string).then(res => res.blob());
|
2019-12-18 18:16:02 +02:00
|
|
|
return { ...file, file: new File([blob], file.name) };
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
await localForage.setItem<BackupEntry>(key, {
|
2019-11-17 11:51:50 +02:00
|
|
|
raw,
|
|
|
|
path: entry.get('path'),
|
2019-12-18 18:16:02 +02:00
|
|
|
mediaFiles,
|
2019-11-17 11:51:50 +02:00
|
|
|
});
|
2019-02-28 13:30:23 -05:00
|
|
|
return localForage.setItem(getEntryBackupKey(), raw);
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
async deleteLocalDraftBackup(collection: Collection, slug: string) {
|
2019-02-28 13:30:23 -05:00
|
|
|
const key = getEntryBackupKey(collection.get('name'), slug);
|
|
|
|
await localForage.removeItem(key);
|
|
|
|
return this.deleteAnonymousBackup();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unnamed backup for use in the global error boundary, should always be
|
|
|
|
// deleted on cms load.
|
|
|
|
deleteAnonymousBackup() {
|
|
|
|
return localForage.removeItem(getEntryBackupKey());
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
async getEntry(state: State, collection: Collection, slug: string) {
|
|
|
|
const path = selectEntryPath(collection, slug) as string;
|
2019-11-08 23:28:17 +00:00
|
|
|
const label = selectFileEntryLabel(collection, slug);
|
2019-12-18 18:16:02 +02:00
|
|
|
|
|
|
|
const integration = selectIntegration(state.integrations, null, 'assetStore');
|
|
|
|
|
2020-01-22 20:42:24 +02:00
|
|
|
const loadedEntry = await this.implementation.getEntry(path);
|
2019-12-18 18:16:02 +02:00
|
|
|
|
|
|
|
const entry = createEntry(collection.get('name'), slug, loadedEntry.file.path, {
|
|
|
|
raw: loadedEntry.data,
|
|
|
|
label,
|
2020-01-22 20:42:24 +02:00
|
|
|
mediaFiles: [],
|
2019-12-18 18:16:02 +02:00
|
|
|
});
|
|
|
|
|
2020-01-22 20:42:24 +02:00
|
|
|
const entryWithFormat = this.entryWithFormat(collection)(entry);
|
|
|
|
if (collection.has('media_folder') && !integration) {
|
|
|
|
entry.mediaFiles = await this.implementation.getMedia(
|
|
|
|
selectMediaFolder(state.config, collection, fromJS(entryWithFormat)),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
entry.mediaFiles = state.mediaLibrary.get('files') || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return entryWithFormat;
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
|
|
|
|
2017-08-14 09:00:47 -04:00
|
|
|
getMedia() {
|
|
|
|
return this.implementation.getMedia();
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
getMediaFile(path: string) {
|
|
|
|
return this.implementation.getMediaFile(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
getMediaDisplayURL(displayURL: DisplayURL) {
|
2019-03-07 21:28:14 -05:00
|
|
|
if (this.implementation.getMediaDisplayURL) {
|
|
|
|
return this.implementation.getMediaDisplayURL(displayURL);
|
|
|
|
}
|
|
|
|
const err = new Error(
|
|
|
|
'getMediaDisplayURL is not implemented by the current backend, but the backend returned a displayURL which was not a string!',
|
2019-12-18 18:16:02 +02:00
|
|
|
) as Error & { displayURL: DisplayURL };
|
2019-03-07 21:28:14 -05:00
|
|
|
err.displayURL = displayURL;
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
entryWithFormat(collectionOrEntity: unknown) {
|
|
|
|
return (entry: EntryValue): EntryValue => {
|
2016-09-06 13:04:17 -03:00
|
|
|
const format = resolveFormat(collectionOrEntity, entry);
|
2017-05-31 16:12:31 -04:00
|
|
|
if (entry && entry.raw !== undefined) {
|
2018-01-29 15:35:36 -07:00
|
|
|
const data = (format && attempt(format.fromFile.bind(format, entry.raw))) || {};
|
2017-06-07 12:47:57 -04:00
|
|
|
if (isError(data)) console.error(data);
|
2017-05-31 16:12:31 -04:00
|
|
|
return Object.assign(entry, { data: isError(data) ? {} : data });
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
2016-10-27 14:45:32 +02:00
|
|
|
return format.fromFile(entry);
|
2016-06-05 01:52:18 -07:00
|
|
|
};
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
2016-06-06 21:53:22 -03:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
unpublishedEntries(collections: Collections) {
|
2020-01-15 00:15:14 +02:00
|
|
|
return this.implementation.unpublishedEntries!()
|
2018-08-07 14:46:54 -06:00
|
|
|
.then(entries =>
|
|
|
|
entries.map(loadedEntry => {
|
2020-01-15 00:15:14 +02:00
|
|
|
const collectionName = loadedEntry.metaData!.collection;
|
2019-11-08 23:28:17 +00:00
|
|
|
const collection = collections.find(c => c.get('name') === collectionName);
|
|
|
|
const entry = createEntry(collectionName, loadedEntry.slug, loadedEntry.file.path, {
|
|
|
|
raw: loadedEntry.data,
|
|
|
|
isModification: loadedEntry.isModification,
|
2020-01-15 00:15:14 +02:00
|
|
|
label: selectFileEntryLabel(collection, loadedEntry.slug!),
|
2019-11-08 23:28:17 +00:00
|
|
|
});
|
2018-08-07 14:46:54 -06:00
|
|
|
entry.metaData = loadedEntry.metaData;
|
|
|
|
return entry;
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.then(entries => ({
|
|
|
|
pagination: 0,
|
|
|
|
entries: entries.reduce((acc, entry) => {
|
|
|
|
const collection = collections.get(entry.collection);
|
|
|
|
if (collection) {
|
2019-12-18 18:16:02 +02:00
|
|
|
acc.push(this.entryWithFormat(collection)(entry) as EntryValue);
|
2017-03-15 18:47:18 -07:00
|
|
|
}
|
2018-08-07 14:46:54 -06:00
|
|
|
return acc;
|
2019-12-18 18:16:02 +02:00
|
|
|
}, [] as EntryValue[]),
|
2018-08-07 14:46:54 -06:00
|
|
|
}));
|
2016-09-06 13:04:17 -03:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
unpublishedEntry(collection: Collection, slug: string) {
|
2020-01-15 00:15:14 +02:00
|
|
|
return this.implementation!.unpublishedEntry!(collection.get('name') as string, slug)
|
2018-08-07 14:46:54 -06:00
|
|
|
.then(loadedEntry => {
|
2019-12-18 18:16:02 +02:00
|
|
|
const entry = createEntry(collection.get('name'), loadedEntry.slug, loadedEntry.file.path, {
|
2017-03-15 18:47:18 -07:00
|
|
|
raw: loadedEntry.data,
|
|
|
|
isModification: loadedEntry.isModification,
|
2019-12-18 18:16:02 +02:00
|
|
|
metaData: loadedEntry.metaData,
|
|
|
|
mediaFiles: loadedEntry.mediaFiles,
|
2017-03-15 18:47:18 -07:00
|
|
|
});
|
2018-08-07 14:46:54 -06:00
|
|
|
return entry;
|
|
|
|
})
|
2019-12-18 18:16:02 +02:00
|
|
|
.then(this.entryWithFormat(collection));
|
2016-09-13 03:59:48 -03:00
|
|
|
}
|
|
|
|
|
2019-02-08 12:26:59 -05:00
|
|
|
/**
|
|
|
|
* Creates a URL using `site_url` from the config and `preview_path` from the
|
|
|
|
* entry's collection. Does not currently make a request through the backend,
|
|
|
|
* but likely will in the future.
|
|
|
|
*/
|
2019-12-18 18:16:02 +02:00
|
|
|
getDeploy(collection: Collection, slug: string, entry: EntryMap) {
|
2019-02-08 12:26:59 -05:00
|
|
|
/**
|
2019-09-24 22:16:09 +02:00
|
|
|
* If `site_url` is undefined or `show_preview_links` in the config is set to false, do nothing.
|
2019-02-08 12:26:59 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
const baseUrl = this.config.get('site_url');
|
|
|
|
|
|
|
|
if (!baseUrl || this.config.get('show_preview_links') === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-01-22 20:42:24 +02:00
|
|
|
url: previewUrlFormatter(baseUrl, collection, slug, this.config.get('slug'), entry),
|
2019-02-08 12:26:59 -05:00
|
|
|
status: 'SUCCESS',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Requests a base URL from the backend for previewing a specific entry.
|
|
|
|
* Supports polling via `maxAttempts` and `interval` options, as there is
|
|
|
|
* often a delay before a preview URL is available.
|
|
|
|
*/
|
2019-12-18 18:16:02 +02:00
|
|
|
async getDeployPreview(
|
|
|
|
collection: Collection,
|
|
|
|
slug: string,
|
|
|
|
entry: EntryMap,
|
|
|
|
{ maxAttempts = 1, interval = 5000 } = {},
|
|
|
|
) {
|
2019-02-08 12:26:59 -05:00
|
|
|
/**
|
|
|
|
* If the registered backend does not provide a `getDeployPreview` method, or
|
|
|
|
* `show_preview_links` in the config is set to false, do nothing.
|
|
|
|
*/
|
|
|
|
if (!this.implementation.getDeployPreview || this.config.get('show_preview_links') === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Poll for the deploy preview URL (defaults to 1 attempt, so no polling by
|
|
|
|
* default).
|
|
|
|
*/
|
|
|
|
let deployPreview,
|
|
|
|
count = 0;
|
|
|
|
while (!deployPreview && count < maxAttempts) {
|
|
|
|
count++;
|
2020-01-15 00:15:14 +02:00
|
|
|
deployPreview = await this.implementation.getDeployPreview(collection.get('name'), slug);
|
2019-02-08 12:26:59 -05:00
|
|
|
if (!deployPreview) {
|
|
|
|
await new Promise(resolve => setTimeout(() => resolve(), interval));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If there's no deploy preview, do nothing.
|
|
|
|
*/
|
|
|
|
if (!deployPreview) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* Create a URL using the collection `preview_path`, if provided.
|
|
|
|
*/
|
2020-01-22 20:42:24 +02:00
|
|
|
url: previewUrlFormatter(deployPreview.url, collection, slug, this.config.get('slug'), entry),
|
2019-02-08 12:26:59 -05:00
|
|
|
/**
|
|
|
|
* Always capitalize the status for consistency.
|
|
|
|
*/
|
|
|
|
status: deployPreview.status ? deployPreview.status.toUpperCase() : '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
async persistEntry({
|
2019-04-10 21:38:53 +01:00
|
|
|
config,
|
|
|
|
collection,
|
|
|
|
entryDraft,
|
2019-12-18 18:16:02 +02:00
|
|
|
assetProxies,
|
2019-04-10 21:38:53 +01:00
|
|
|
usedSlugs,
|
2019-12-18 18:16:02 +02:00
|
|
|
unpublished = false,
|
|
|
|
status,
|
|
|
|
}: PersistArgs) {
|
2018-08-07 14:46:54 -06:00
|
|
|
const newEntry = entryDraft.getIn(['entry', 'newRecord']) || false;
|
2016-09-05 12:12:38 -03:00
|
|
|
|
|
|
|
const parsedData = {
|
2020-01-22 20:42:24 +02:00
|
|
|
title: entryDraft.getIn(['entry', 'data', 'title'], 'No Title') as string,
|
|
|
|
description: entryDraft.getIn(['entry', 'data', 'description'], 'No Description!') as string,
|
2016-09-05 12:12:38 -03:00
|
|
|
};
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
let entryObj: {
|
|
|
|
path: string;
|
|
|
|
slug: string;
|
|
|
|
raw: string;
|
|
|
|
};
|
|
|
|
|
2016-08-25 16:11:00 -03:00
|
|
|
if (newEntry) {
|
2016-11-11 17:54:58 -02:00
|
|
|
if (!selectAllowNewEntries(collection)) {
|
2018-08-07 14:46:54 -06:00
|
|
|
throw new Error('Not allowed to create new entries in this collection');
|
2016-10-27 14:23:36 +02:00
|
|
|
}
|
2019-04-10 21:38:53 +01:00
|
|
|
const slug = await this.generateUniqueSlug(
|
2018-08-07 14:46:54 -06:00
|
|
|
collection,
|
|
|
|
entryDraft.getIn(['entry', 'data']),
|
2020-01-15 00:15:14 +02:00
|
|
|
config,
|
2019-04-10 21:38:53 +01:00
|
|
|
usedSlugs,
|
2018-08-07 14:46:54 -06:00
|
|
|
);
|
2019-12-18 18:16:02 +02:00
|
|
|
const path = selectEntryPath(collection, slug) as string;
|
|
|
|
|
2016-08-25 16:11:00 -03:00
|
|
|
entryObj = {
|
2016-10-28 04:51:37 +02:00
|
|
|
path,
|
2016-10-21 20:42:14 -02:00
|
|
|
slug,
|
2018-08-07 14:46:54 -06:00
|
|
|
raw: this.entryToRaw(collection, entryDraft.get('entry')),
|
2016-08-25 16:11:00 -03:00
|
|
|
};
|
2019-12-18 18:16:02 +02:00
|
|
|
|
|
|
|
assetProxies.map(asset => {
|
|
|
|
// update media files path based on entry path
|
|
|
|
const oldPath = asset.path;
|
2020-01-29 18:54:01 +01:00
|
|
|
const newPath = selectMediaFilePath(
|
|
|
|
config,
|
|
|
|
collection,
|
|
|
|
entryDraft.get('entry').set('path', path),
|
|
|
|
oldPath,
|
|
|
|
);
|
2019-12-18 18:16:02 +02:00
|
|
|
asset.path = newPath;
|
|
|
|
});
|
2016-08-25 16:11:00 -03:00
|
|
|
} else {
|
2018-08-07 14:46:54 -06:00
|
|
|
const path = entryDraft.getIn(['entry', 'path']);
|
|
|
|
const slug = entryDraft.getIn(['entry', 'slug']);
|
2016-08-25 16:11:00 -03:00
|
|
|
entryObj = {
|
2016-10-28 04:51:37 +02:00
|
|
|
path,
|
2017-02-21 23:04:12 -08:00
|
|
|
slug,
|
2018-08-07 14:46:54 -06:00
|
|
|
raw: this.entryToRaw(collection, entryDraft.get('entry')),
|
2016-08-25 16:11:00 -03:00
|
|
|
};
|
|
|
|
}
|
2016-07-19 17:11:22 -03:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
const user = (await this.currentUser()) as User;
|
2019-10-28 21:29:47 +02:00
|
|
|
const commitMessage = commitMessageFormatter(
|
|
|
|
newEntry ? 'create' : 'update',
|
|
|
|
config,
|
|
|
|
{
|
|
|
|
collection,
|
|
|
|
slug: entryObj.slug,
|
|
|
|
path: entryObj.path,
|
|
|
|
authorLogin: user.login,
|
|
|
|
authorName: user.name,
|
|
|
|
},
|
|
|
|
user.useOpenAuthoring,
|
|
|
|
);
|
2016-07-19 17:11:22 -03:00
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
const useWorkflow = selectUseWorkflow(config);
|
2018-08-01 15:06:22 -04:00
|
|
|
|
2018-08-07 14:46:54 -06:00
|
|
|
const collectionName = collection.get('name');
|
2016-08-30 19:06:20 -03:00
|
|
|
|
2019-12-20 19:33:43 +02:00
|
|
|
const updatedOptions = { unpublished, status };
|
2018-07-17 18:09:59 -04:00
|
|
|
const opts = {
|
|
|
|
newEntry,
|
|
|
|
parsedData,
|
|
|
|
commitMessage,
|
|
|
|
collectionName,
|
2018-08-01 15:06:22 -04:00
|
|
|
useWorkflow,
|
2018-08-07 14:46:54 -06:00
|
|
|
...updatedOptions,
|
2018-07-17 18:09:59 -04:00
|
|
|
};
|
2017-11-11 09:48:47 -05:00
|
|
|
|
2020-01-31 16:44:01 -08:00
|
|
|
if (!useWorkflow) {
|
|
|
|
await this.invokePrePublishEvent(entryDraft.get('entry'));
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.implementation.persistEntry(entryObj, assetProxies, opts);
|
|
|
|
|
|
|
|
if (!useWorkflow) {
|
|
|
|
await this.invokePostPublishEvent(entryDraft.get('entry'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return entryObj.slug;
|
|
|
|
}
|
|
|
|
|
|
|
|
async invokePrePublishEvent(entry: EntryMap) {
|
|
|
|
const { login, name } = (await this.currentUser()) as User;
|
|
|
|
await invokeEvent({ name: 'prePublish', data: { entry, author: { login, name } } });
|
|
|
|
}
|
|
|
|
|
|
|
|
async invokePostPublishEvent(entry: EntryMap) {
|
|
|
|
const { login, name } = (await this.currentUser()) as User;
|
|
|
|
await invokeEvent({ name: 'postPublish', data: { entry, author: { login, name } } });
|
2016-06-06 21:53:22 -03:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
async persistMedia(config: Config, file: AssetProxy) {
|
|
|
|
const user = (await this.currentUser()) as User;
|
2017-08-14 09:00:47 -04:00
|
|
|
const options = {
|
2019-10-28 21:29:47 +02:00
|
|
|
commitMessage: commitMessageFormatter(
|
|
|
|
'uploadMedia',
|
|
|
|
config,
|
|
|
|
{
|
|
|
|
path: file.path,
|
|
|
|
authorLogin: user.login,
|
|
|
|
authorName: user.name,
|
|
|
|
},
|
|
|
|
user.useOpenAuthoring,
|
|
|
|
),
|
2017-08-14 09:00:47 -04:00
|
|
|
};
|
|
|
|
return this.implementation.persistMedia(file, options);
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
async deleteEntry(config: Config, collection: Collection, slug: string) {
|
|
|
|
const path = selectEntryPath(collection, slug) as string;
|
2017-10-30 20:07:54 +00:00
|
|
|
|
|
|
|
if (!selectAllowDeletion(collection)) {
|
2018-08-07 14:46:54 -06:00
|
|
|
throw new Error('Not allowed to delete entries in this collection');
|
2017-10-30 20:07:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
const user = (await this.currentUser()) as User;
|
2019-10-28 21:29:47 +02:00
|
|
|
const commitMessage = commitMessageFormatter(
|
|
|
|
'delete',
|
|
|
|
config,
|
|
|
|
{
|
|
|
|
collection,
|
|
|
|
slug,
|
|
|
|
path,
|
|
|
|
authorLogin: user.login,
|
|
|
|
authorName: user.name,
|
|
|
|
},
|
|
|
|
user.useOpenAuthoring,
|
|
|
|
);
|
2020-01-15 00:15:14 +02:00
|
|
|
return this.implementation.deleteFile(path, commitMessage);
|
2017-07-21 23:40:33 -07:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
async deleteMedia(config: Config, path: string) {
|
|
|
|
const user = (await this.currentUser()) as User;
|
2019-10-28 21:29:47 +02:00
|
|
|
const commitMessage = commitMessageFormatter(
|
|
|
|
'deleteMedia',
|
|
|
|
config,
|
|
|
|
{
|
|
|
|
path,
|
|
|
|
authorLogin: user.login,
|
|
|
|
authorName: user.name,
|
|
|
|
},
|
|
|
|
user.useOpenAuthoring,
|
|
|
|
);
|
2017-08-14 09:00:47 -04:00
|
|
|
return this.implementation.deleteFile(path, commitMessage);
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
persistUnpublishedEntry(args: PersistArgs) {
|
|
|
|
return this.persistEntry({ ...args, unpublished: true });
|
2016-09-13 16:00:24 -03:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
updateUnpublishedEntryStatus(collection: string, slug: string, newStatus: string) {
|
2020-01-15 00:15:14 +02:00
|
|
|
return this.implementation.updateUnpublishedEntryStatus!(collection, slug, newStatus);
|
2016-09-13 14:31:18 -03:00
|
|
|
}
|
|
|
|
|
2020-01-31 16:44:01 -08:00
|
|
|
async publishUnpublishedEntry(entry: EntryMap) {
|
|
|
|
const collection = entry.get('collection');
|
|
|
|
const slug = entry.get('slug');
|
|
|
|
|
|
|
|
await this.invokePrePublishEvent(entry);
|
|
|
|
await this.implementation.publishUnpublishedEntry!(collection, slug);
|
|
|
|
await this.invokePostPublishEvent(entry);
|
2016-09-14 18:25:45 -03:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
deleteUnpublishedEntry(collection: string, slug: string) {
|
2020-01-15 00:15:14 +02:00
|
|
|
return this.implementation.deleteUnpublishedEntry!(collection, slug);
|
2017-03-11 13:47:36 -05:00
|
|
|
}
|
2016-09-13 14:31:18 -03:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
entryToRaw(collection: Collection, entry: EntryMap): string {
|
2017-02-21 23:04:12 -08:00
|
|
|
const format = resolveFormat(collection, entry.toJS());
|
|
|
|
const fieldsOrder = this.fieldsOrder(collection, entry);
|
2018-08-07 14:46:54 -06:00
|
|
|
return format && format.toFile(entry.get('data').toJS(), fieldsOrder);
|
2017-02-21 23:04:12 -08:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
fieldsOrder(collection: Collection, entry: EntryMap) {
|
2017-02-21 23:04:12 -08:00
|
|
|
const fields = collection.get('fields');
|
|
|
|
if (fields) {
|
2018-08-07 14:46:54 -06:00
|
|
|
return collection
|
|
|
|
.get('fields')
|
2020-01-15 00:15:14 +02:00
|
|
|
.map(f => f!.get('name'))
|
2018-08-07 14:46:54 -06:00
|
|
|
.toArray();
|
2017-02-21 23:04:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const files = collection.get('files');
|
2019-12-18 18:16:02 +02:00
|
|
|
const file = (files || List<CollectionFile>())
|
2020-01-15 00:15:14 +02:00
|
|
|
.filter(f => f!.get('name') === entry.get('slug'))
|
2019-12-18 18:16:02 +02:00
|
|
|
.get(0);
|
|
|
|
|
2017-02-21 23:04:12 -08:00
|
|
|
if (file == null) {
|
2018-08-07 14:46:54 -06:00
|
|
|
throw new Error(`No file found for ${entry.get('slug')} in ${collection.get('name')}`);
|
2017-02-21 23:04:12 -08:00
|
|
|
}
|
2018-08-07 14:46:54 -06:00
|
|
|
return file
|
|
|
|
.get('fields')
|
2020-01-15 00:15:14 +02:00
|
|
|
.map(f => f!.get('name'))
|
2018-08-07 14:46:54 -06:00
|
|
|
.toArray();
|
2016-06-06 21:53:22 -03:00
|
|
|
}
|
2017-05-25 17:24:48 +01:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
filterEntries(collection: { entries: EntryValue[] }, filterRule: FilterRule) {
|
2018-10-23 21:51:11 -04:00
|
|
|
return collection.entries.filter(entry => {
|
|
|
|
const fieldValue = entry.data[filterRule.get('field')];
|
|
|
|
if (Array.isArray(fieldValue)) {
|
|
|
|
return fieldValue.includes(filterRule.get('value'));
|
|
|
|
}
|
|
|
|
return fieldValue === filterRule.get('value');
|
|
|
|
});
|
2017-05-25 17:24:48 +01:00
|
|
|
}
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function resolveBackend(config: Config) {
|
2018-08-07 14:46:54 -06:00
|
|
|
const name = config.getIn(['backend', 'name']);
|
2016-05-30 16:55:32 -07:00
|
|
|
if (name == null) {
|
2018-08-07 14:46:54 -06:00
|
|
|
throw new Error('No backend defined in configuration');
|
2016-05-30 16:55:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const authStore = new LocalStorageAuthStore();
|
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
const backend = getBackend(name);
|
|
|
|
if (!backend) {
|
2018-08-07 14:46:54 -06:00
|
|
|
throw new Error(`Backend not found: ${name}`);
|
2018-01-25 08:55:13 -08:00
|
|
|
} else {
|
2020-01-15 00:15:14 +02:00
|
|
|
return new Backend(backend, { backendName: name, authStore, config });
|
2016-05-30 16:55:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 14:46:54 -06:00
|
|
|
export const currentBackend = (function() {
|
2019-12-18 18:16:02 +02:00
|
|
|
let backend: Backend;
|
2016-02-25 12:31:21 -08:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
return (config: Config) => {
|
2018-08-07 14:46:54 -06:00
|
|
|
if (backend) {
|
|
|
|
return backend;
|
|
|
|
}
|
2019-12-18 18:16:02 +02:00
|
|
|
|
|
|
|
return (backend = resolveBackend(config));
|
2016-02-25 12:31:21 -08:00
|
|
|
};
|
2018-08-07 14:46:54 -06:00
|
|
|
})();
|