- Rebuilt the entire project - now we can pass blockName in URL.
- Added "ViewMode"
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import {getBlockConfigs, getBlockName, getBlockVariations, handlebarLayoutsPath} from "../helpers.js";
|
||||
import path from "path";
|
||||
import {buildAssetFiles} from "../inc/changes-watcher.js";
|
||||
|
||||
export default async function (req, res) {
|
||||
const blockName = req.query.block;
|
||||
const block = getBlockName(blockName);
|
||||
|
||||
const blockDir = path.join('blocks', '@' + block.project, block.name);
|
||||
const dataFiles = await getBlockVariations(blockDir);
|
||||
|
||||
const data = await getDataOfFrame(!!req.query.iframe, Object.assign({}, block, {dataFiles}));
|
||||
|
||||
if (data.error && data.errorMessage) {
|
||||
return res.send(data.errorMessage);
|
||||
}
|
||||
|
||||
const baseView = req.params.baseView ?? 'alignfull';
|
||||
|
||||
await buildAssetFiles(blockDir);
|
||||
|
||||
res.render(baseView, data)
|
||||
}
|
||||
|
||||
async function getDataOfFrame(isIframe = false, block) {
|
||||
const data = {config: await getBlockConfigs(block)};
|
||||
if (data.error && data.errorMessage) {
|
||||
return data;
|
||||
}
|
||||
|
||||
data.helpers = {
|
||||
include_partial: (filesPath) => handlebarLayoutsPath(filesPath),
|
||||
}
|
||||
|
||||
if (isIframe) {
|
||||
data.iframeMode = true;
|
||||
}
|
||||
|
||||
data.staticFilesPath = `/block/${block.project}/${block.name}`;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import {
|
||||
getBlockData,
|
||||
getBlockName,
|
||||
getBlockVariations,
|
||||
getListOfDesignPreviewFiles,
|
||||
} from "../helpers.js";
|
||||
import path from "path";
|
||||
import fs from "fs/promises";
|
||||
|
||||
export default async function (req, res, next) {
|
||||
let variationName = req.query.name ? req.query.name : 'default';
|
||||
const block = getBlockName(req.query.block);
|
||||
|
||||
const blockDir = path.join('blocks', '@' + block.project, block.name);
|
||||
const dataFiles = await getBlockVariations(blockDir);
|
||||
const data = await getBlockData(variationName, {projectPath: blockDir});
|
||||
|
||||
let designPreviewFiles = [];
|
||||
try {
|
||||
designPreviewFiles = getListOfDesignPreviewFiles(variationName, await fs.readdir(path.join(blockDir, 'design', 'preview')), block);
|
||||
} catch (err) {
|
||||
console.log('Preview Design doesn\'t exist');
|
||||
}
|
||||
|
||||
return res.json({
|
||||
dataOptions: dataFiles,
|
||||
designPreview: designPreviewFiles,
|
||||
data,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import {getBlockConfigs, getBlockName, getBlockVariations, handlebarLayoutsPath, verifyVersion} from "../helpers.js";
|
||||
import path from "path";
|
||||
import {REGISTRY_URL} from "@axe-web/create-block/env.js";
|
||||
|
||||
export async function index(req, res, next) {
|
||||
|
||||
const blockName = req.query.block;
|
||||
const block = getBlockName(blockName);
|
||||
|
||||
const blockDir = path.join('blocks', '@' + block.project, block.name);
|
||||
const dataFiles = await getBlockVariations(blockDir);
|
||||
|
||||
const data = await getBlockConfigs(Object.assign({}, block, {dataFiles}));
|
||||
|
||||
if (data.error && data.errorMessage) {
|
||||
// TODO: Throw Error.
|
||||
}
|
||||
|
||||
data.helpers = {
|
||||
include_partial: (filesPath) => handlebarLayoutsPath('.', filesPath),
|
||||
}
|
||||
|
||||
// TODO: Make sure we sync block before we start working on it.
|
||||
try {
|
||||
const verifiedVersion = await verifyVersion(blockName, blockDir, REGISTRY_URL);
|
||||
if (!verifiedVersion && !req.query['ignoreVersionSync']) {
|
||||
return res.render('sync', data);
|
||||
}
|
||||
} catch (err) {
|
||||
const errorMessage = "Can't verify block version.";
|
||||
console.log(errorMessage, err);
|
||||
return next(new Error(errorMessage));
|
||||
}
|
||||
|
||||
data.blockName = blockName;
|
||||
data.baseView = 'alignfull';
|
||||
data.port = `/view/${data.baseView}`;
|
||||
|
||||
let previewFrameUrl = ``; // This variable is used in `*.hbs` and it will be updated once BrowserSync is ready.
|
||||
data.previewFrameUrl = `${previewFrameUrl}${data.port}`;
|
||||
data.previewFrameUrlNewWindow = `${previewFrameUrl}${data.port}`;
|
||||
data.shareUrl = '';
|
||||
|
||||
data.viewMode = process.env.VIEW_MODE ?? false;
|
||||
|
||||
res.render('index', data);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export default async function (req, res, next) {
|
||||
|
||||
if (process.env.VIEW_MODE) {
|
||||
throw new Error("Can't publish in view mode.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
export function staticFiles(req, res) {
|
||||
const fileName = req.params[0];
|
||||
const path = `blocks/@${req.params.project}/${req.params.blockName}/src/${fileName}`;
|
||||
|
||||
return deliverStaticFile(path, res);
|
||||
}
|
||||
|
||||
export function previewFiles(req, res) {
|
||||
const fileName = req.params[0];
|
||||
const path = `blocks/@${req.params.project}/${req.params.blockName}/design/${fileName}`;
|
||||
|
||||
return deliverStaticFile(path, res);
|
||||
}
|
||||
|
||||
function deliverStaticFile(path, res) {
|
||||
// If file doesn't exist, return 404.
|
||||
res.sendFile(path, {root: './'}, function (err) {
|
||||
if (err) {
|
||||
res.send('File not found.');
|
||||
res.status(err.status).end();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import {syncFilesWithCloud} from "../helpers.js";
|
||||
|
||||
export default async function (req, res, next) {
|
||||
try {
|
||||
await syncFilesWithCloud(req.body.block, null, true);
|
||||
} catch (err) {
|
||||
return res.status(500).json({status: 500, message: err.message});
|
||||
}
|
||||
|
||||
res.json({status: 200, message: 'Successfully synced!'});
|
||||
}
|
||||
Reference in New Issue
Block a user