Skip to content

Commit

Permalink
#743 - Fix for storing yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Jan 26, 2024
1 parent 7240747 commit 5f92ad3
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 72 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [#725](https://github.com/estruyf/vscode-front-matter/issues/725): Fix for opening menu of pinned items
- [#730](https://github.com/estruyf/vscode-front-matter/issues/730): Add debounce to the input fields
- [#738](https://github.com/estruyf/vscode-front-matter/issues/738): Fix when re-opening the preview after closing it
- [#743](https://github.com/estruyf/vscode-front-matter/issues/743): Fix for storing data in YAML data files

## [9.4.0] - 2023-12-12 - [Release notes](https://beta.frontmatter.codes/updates/v9.4.0)

Expand Down
48 changes: 8 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2566,7 +2566,7 @@
"@types/chai": "^4.3.4",
"@types/glob": "7.1.3",
"@types/invariant": "^2.2.35",
"@types/js-yaml": "3.12.1",
"@types/js-yaml": "^4.0.9",
"@types/lodash.omit": "^4.5.7",
"@types/lodash.uniqby": "4.7.6",
"@types/lodash.xor": "^4.5.7",
Expand Down Expand Up @@ -2633,7 +2633,6 @@
"react-quill": "^2.0.0",
"react-router-dom": "^6.8.0",
"react-sortable-hoc": "^2.0.0",
"react-toastify": "^8.2.0",
"recoil": "^0.4.1",
"remark-gfm": "^3.0.1",
"rimraf": "^3.0.2",
Expand Down
1 change: 1 addition & 0 deletions src/dashboardWebView/DashboardMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export enum DashboardMessage {
runCustomScript = 'runCustomScript',
sendTelemetry = 'sendTelemetry',
logError = 'logError',
showNotification = 'showNotification',

// Settings
getSettings = 'getSettings',
Expand Down
17 changes: 2 additions & 15 deletions src/dashboardWebView/components/DataView/DataView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,17 @@ import { EmptyView } from './EmptyView';
import { Container } from './SortableContainer';
import { SortableItem } from './SortableItem';
import { ChevronRightIcon, CircleStackIcon } from '@heroicons/react/24/outline';
import { ToastContainer, toast, Slide } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { DataType } from '../../../models/DataType';
import { TelemetryEvent } from '../../../constants';
import { NavigationItem } from '../Layout';
import useThemeColors from '../../hooks/useThemeColors';
import * as l10n from '@vscode/l10n';
import { LocalizationKey } from '../../../localization';
import { NavigationType } from '../../models';

export interface IDataViewProps { }

export const DataView: React.FunctionComponent<IDataViewProps> = (
props: React.PropsWithChildren<IDataViewProps>
_: React.PropsWithChildren<IDataViewProps>
) => {
const [selectedData, setSelectedData] = useState<DataFile | null>(null);
const [selectedIndex, setSelectedIndex] = useState<number | null>(null);
Expand Down Expand Up @@ -112,15 +109,7 @@ export const DataView: React.FunctionComponent<IDataViewProps> = (
entries: data
});

// Show toast message
toast.success('Updated your data entries', {
position: 'top-right',
autoClose: 2000,
hideProgressBar: true,
closeOnClick: true,
pauseOnHover: false,
transition: Slide
});
Messenger.send(DashboardMessage.showNotification, 'Updated your data entries');
},
[selectedData]
);
Expand Down Expand Up @@ -319,8 +308,6 @@ export const DataView: React.FunctionComponent<IDataViewProps> = (
isBacker={settings?.isBacker}
/>

<ToastContainer />

<img className='hidden' src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Ffrontmatter.codes%2Fmetrics%2Fdashboards&slug=DataView" alt="DataView metrics" />
</div >
);
Expand Down
14 changes: 4 additions & 10 deletions src/dashboardWebView/components/DataView/SortableItem.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PencilIcon, ChevronDownIcon, TrashIcon } from '@heroicons/react/24/outline';
import { PencilIcon, TrashIcon, ChevronUpDownIcon } from '@heroicons/react/24/outline';
import * as React from 'react';
import { SortableHandle, SortableElement } from 'react-sortable-hoc';
import useThemeColors from '../../hooks/useThemeColors';
import { LinkButton } from '../Common/LinkButton';
import { Alert } from '../Modals/Alert';
import * as l10n from '@vscode/l10n';
Expand All @@ -16,7 +15,7 @@ export interface ISortableItemProps {
onDeleteItem: (index: number) => void;
}

const DragHandle = SortableHandle(() => <ChevronDownIcon className={`w-6 h-6 cursor-move hover:text-[var(--frontmatter-link-hover)]`} />);
const DragHandle = SortableHandle(() => <ChevronUpDownIcon className={`w-6 h-6 mr-2 cursor-move hover:text-[var(--frontmatter-link-hover)]`} />);

export const SortableItem = SortableElement(
({
Expand All @@ -27,7 +26,6 @@ export const SortableItem = SortableElement(
onDeleteItem
}: ISortableItemProps) => {
const [showAlert, setShowAlert] = React.useState(false);
const { getColors } = useThemeColors();

const deleteItemConfirm = () => {
setShowAlert(true);
Expand All @@ -37,12 +35,8 @@ export const SortableItem = SortableElement(
<>
<li
data-test={`${selectedIndex}-${crntIndex}`}
className={`sortable_item py-2 px-2 w-full flex justify-between content-center cursor-pointer ${selectedIndex === crntIndex ? getColors(`bg-gray-300 dark:bg-vulcan-300`, `bg-[var(--frontmatter-list-selected-background)] text-[var(--frontmatter-list-selected-text)]`) : ``
} ${getColors(
'hover:bg-gray-200 dark:hover:bg-vulcan-400',
'hover:bg-[var(--frontmatter-list-hover-background)]'
)
}`}
className={`sortable_item py-2 px-2 w-full flex justify-between content-center cursor-pointer ${selectedIndex === crntIndex ? `bg-[var(--frontmatter-list-selected-background)] text-[var(--frontmatter-list-selected-text)]` : ``
} hover:bg-[var(--frontmatter-list-hover-background)]`}
>
<div
className="flex items-center w-full"
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/FrameworkDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class FrameworkDetector {
if (await existsAsync(hexoConfig)) {
const content = await readFileAsync(hexoConfig, 'utf8');
// Convert YAML to JSON
const config = jsyaml.load(content);
const config = jsyaml.load(content) as any;

// Check if post assets are used: https://hexo.io/docs/asset-folders.html#Post-Asset-Folder
if (config.post_asset_folder) {
Expand Down Expand Up @@ -210,7 +210,7 @@ export class FrameworkDetector {
if (await existsAsync(jekyllConfig)) {
const content = await readFileAsync(jekyllConfig, 'utf8');
// Convert YAML to JSON
const config = jsyaml.load(content);
const config = jsyaml.load(content) as any;

if (config.collections_dir) {
collectionDir = config.collections_dir;
Expand Down
5 changes: 5 additions & 0 deletions src/listeners/dashboard/DashboardListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export class DashboardListener extends BaseListener {
case DashboardMessage.getPinnedItems:
DashboardListener.getPinnedItems(msg);
break;
case DashboardMessage.showNotification:
if (msg.payload) {
Notifications.info(msg.payload);
}
break;
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/listeners/dashboard/DataListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DashboardCommand } from '../../dashboardWebView/DashboardCommand';
import { Folders } from '../../commands/Folders';
import { dirname } from 'path';
import * as yaml from 'js-yaml';
import { DataFileHelper } from '../../helpers';
import { DataFileHelper, Logger } from '../../helpers';
import { existsAsync, readFileAsync, writeFileAsync } from '../../utils';
import { mkdirAsync } from '../../utils/mkdirAsync';
import { PostMessageData } from '../../models';
Expand Down Expand Up @@ -59,8 +59,12 @@ export class DataListener extends BaseListener {
newFileContent || workspace.getConfiguration().get('files.insertFinalNewline');

if (fileType === 'yaml') {
const yamlData = yaml.safeDump(entries);
await writeFileAsync(absPath, insertFinalNewLine ? `${yamlData}\n` : yamlData, 'utf8');
try {
const yamlData = yaml.dump(entries);
await writeFileAsync(absPath, insertFinalNewLine ? `${yamlData}\n` : yamlData, 'utf8');
} catch (e) {
Logger.error((e as Error).message);
}
} else {
const jsonData = JSON.stringify(entries, null, 2);
await writeFileAsync(absPath, insertFinalNewLine ? `${jsonData}\n` : jsonData, 'utf8');
Expand Down

0 comments on commit 5f92ad3

Please sign in to comment.