myst-cli
Version:
Command line tools for MyST
173 lines (172 loc) • 6.41 kB
JavaScript
import { resolve } from 'node:path';
import { createSlice } from '@reduxjs/toolkit';
import { combineReducers } from 'redux';
export const projects = createSlice({
name: 'projects',
initialState: {},
reducers: {
receive(state, action) {
state[resolve(action.payload.path)] = action.payload;
},
},
});
export const affiliations = createSlice({
name: 'affiliations',
initialState: {},
reducers: {
receive(state, action) {
action.payload.affiliations.forEach((aff) => {
state[aff.id] = aff.text;
});
},
},
});
export const config = createSlice({
name: 'config',
initialState: {
rawConfigs: {},
projects: {},
projectParts: {},
fileParts: {},
sites: {},
filenames: {},
},
reducers: {
receiveCurrentProjectPath(state, action) {
state.currentProjectPath = resolve(action.payload.path);
},
receiveCurrentSitePath(state, action) {
state.currentSitePath = resolve(action.payload.path);
},
receiveRawConfig(state, action) {
const { path, file, ...payload } = action.payload;
state.rawConfigs[resolve(path)] = payload;
state.filenames[resolve(path)] = file;
},
receiveSiteConfig(state, action) {
const { path, ...payload } = action.payload;
state.sites[resolve(path)] = payload;
},
receiveProjectConfig(state, action) {
const { path, ...payload } = action.payload;
state.projects[resolve(path)] = payload;
},
receiveConfigExtension(state, action) {
var _a;
(_a = state.configExtensions) !== null && _a !== void 0 ? _a : (state.configExtensions = []);
state.configExtensions.push(action.payload.file);
},
receiveProjectPart(state, action) {
var _a;
const { path, partFile } = action.payload;
const partFiles = (_a = state.projectParts[resolve(path)]) !== null && _a !== void 0 ? _a : [];
if (!partFiles.includes(partFile)) {
state.projectParts[resolve(path)] = [...partFiles, partFile];
}
},
receiveFilePart(state, action) {
var _a;
const { file, partFile } = action.payload;
const partFiles = (_a = state.fileParts[resolve(file)]) !== null && _a !== void 0 ? _a : [];
if (!partFiles.includes(partFile)) {
state.fileParts[resolve(file)] = [...partFiles, partFile];
}
},
},
});
export const watch = createSlice({
name: 'watch',
initialState: { files: {}, reloading: false },
reducers: {
markReloading(state, action) {
state.reloading = action.payload;
},
markReloadRequested(state, action) {
state.reloadRequested = action.payload;
},
markFileChanged(state, action) {
const { path, sha256 = null } = action.payload;
state.files[resolve(path)] = { ...state.files[resolve(path)], sha256 };
},
addLocalDependency(state, action) {
var _a;
const { path, dependency } = action.payload;
if (!state.files[resolve(path)])
state.files[resolve(path)] = {};
const existingDeps = [...((_a = state.files[resolve(path)].localDependencies) !== null && _a !== void 0 ? _a : [])];
if (!existingDeps.includes(dependency)) {
state.files[resolve(path)].localDependencies = [...existingDeps, dependency];
}
},
updateFileInfo(state, action) {
const { path, sha256, title, short_title, description, date, thumbnail, thumbnailOptimized, banner, bannerOptimized, tags, url, dataUrl, } = action.payload;
const resolvedPath = resolve(path);
if (!state.files[resolvedPath])
state.files[resolvedPath] = {};
if (title)
state.files[resolvedPath].title = title;
if (short_title)
state.files[resolvedPath].short_title = short_title;
if (description)
state.files[resolvedPath].description = description;
if (date)
state.files[resolvedPath].date = date;
if (thumbnail)
state.files[resolvedPath].thumbnail = thumbnail;
if (thumbnailOptimized)
state.files[resolvedPath].thumbnailOptimized = thumbnailOptimized;
if (banner)
state.files[resolvedPath].banner = banner;
if (bannerOptimized)
state.files[resolvedPath].bannerOptimized = bannerOptimized;
if (tags)
state.files[resolvedPath].tags = [...tags];
if (sha256)
state.files[resolvedPath].sha256 = sha256;
if (url)
state.files[resolvedPath].url = url;
if (dataUrl)
state.files[resolvedPath].dataUrl = dataUrl;
},
},
});
export const links = createSlice({
name: 'links',
initialState: {},
reducers: {
updateLink(state, action) {
const { url, ok, skipped, status, statusText } = action.payload;
state[url] = { url, ok, skipped, status, statusText };
},
},
});
export const warnings = createSlice({
name: 'warnings',
initialState: {},
reducers: {
addWarning(state, action) {
var _a;
const { file, message, kind, url, note, position, ruleId } = action.payload;
state[file] = [...((_a = state[file]) !== null && _a !== void 0 ? _a : []), { message, kind, url, note, position, ruleId }];
},
clearWarnings(state, action) {
state[action.payload.file] = [];
},
clearAllWarnings(state) {
Object.keys(state).forEach((key) => {
delete state[key];
});
},
},
});
export const localReducer = combineReducers({
projects: projects.reducer,
affiliations: affiliations.reducer,
config: config.reducer,
watch: watch.reducer,
links: links.reducer,
warnings: warnings.reducer,
});
export const rootReducer = combineReducers({
local: localReducer,
});