refactor: Address review comments

Moved some types around
Removed unnecessary params

Co-authored-by: Reda Al Sulais <u.yokozuna@gmail.com>
Co-authored-by: Alois Klink <alois@aloisklink.com>
This commit is contained in:
Sidharth Vinod
2023-11-14 11:48:11 +05:30
parent ce6bfcb7f5
commit 7c79bbd6b0
7 changed files with 38 additions and 22 deletions

View File

@@ -1,4 +1,4 @@
import { imgSnapshotTest, renderGraph } from '../../helpers/util'; import { imgSnapshotTest } from '../../helpers/util';
describe('packet structure', () => { describe('packet structure', () => {
it('should render a simple packet diagram', () => { it('should render a simple packet diagram', () => {

View File

@@ -14,7 +14,7 @@
#### Defined in #### Defined in
[defaultConfig.ts:272](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L272) [defaultConfig.ts:275](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L275)
--- ---

View File

@@ -259,10 +259,6 @@ const config: RequiredDeep<MermaidConfig> = {
}, },
packet: { packet: {
...defaultConfigJson.packet, ...defaultConfigJson.packet,
useWidth: undefined,
// this is false, unlike every other diagram (other than gitGraph)
// TODO: can we make this default to `true` instead?
useMaxWidth: false,
}, },
}; };

View File

@@ -1,13 +1,9 @@
import type { Block, PacketDB, Row } from './types.js'; import type { Block, PacketDB, PacketData, Row } from './types.js';
import type { PacketDiagramConfig } from '../../config.type.js'; import type { PacketDiagramConfig } from '../../config.type.js';
import { log } from '../../logger.js'; import { log } from '../../logger.js';
import DEFAULT_CONFIG from '../../defaultConfig.js'; import DEFAULT_CONFIG from '../../defaultConfig.js';
import { getConfig as commonGetConfig } from '../../config.js'; import { getConfig as commonGetConfig } from '../../config.js';
import { cleanAndMerge } from '../../utils.js';
interface PacketData {
packet: Row[];
}
const defaultPacketData: PacketData = { const defaultPacketData: PacketData = {
packet: [], packet: [],
@@ -17,7 +13,7 @@ let data: PacketData = structuredClone(defaultPacketData);
export const DEFAULT_PACKET_CONFIG: Required<PacketDiagramConfig> = DEFAULT_CONFIG.packet; export const DEFAULT_PACKET_CONFIG: Required<PacketDiagramConfig> = DEFAULT_CONFIG.packet;
export const getConfig = (): Required<PacketDiagramConfig> => { export const getConfig = (): Required<PacketDiagramConfig> => {
const config = structuredClone({ const config = cleanAndMerge({
...DEFAULT_PACKET_CONFIG, ...DEFAULT_PACKET_CONFIG,
...commonGetConfig().packet, ...commonGetConfig().packet,
}); });
@@ -61,9 +57,9 @@ export const getNextFittingBlock = (
}; };
export const populate = ({ blocks }: { blocks: Block[] }) => { export const populate = ({ blocks }: { blocks: Block[] }) => {
clear();
let lastByte = -1; let lastByte = -1;
let word: Row = []; let word: Row = [];
data.packet = [];
let row = 1; let row = 1;
const { bitsPerRow } = getConfig(); const { bitsPerRow } = getConfig();
for (let { start, end, label } of blocks) { for (let { start, end, label } of blocks) {
@@ -97,7 +93,6 @@ export const populate = ({ blocks }: { blocks: Block[] }) => {
if (word.length > 0) { if (word.length > 0) {
data.packet.push(word); data.packet.push(word);
} }
log.debug(data);
}; };
export const clear = () => { export const clear = () => {
@@ -107,4 +102,5 @@ export const clear = () => {
export const db: PacketDB = { export const db: PacketDB = {
getPacket, getPacket,
getConfig, getConfig,
clear,
}; };

View File

@@ -1,6 +1,8 @@
import type { DiagramStylesProvider } from '../../diagram-api/types.js';
import { log } from '../../logger.js'; import { log } from '../../logger.js';
import type { PacketStyleOptions } from './types.js';
export const styles = (options: any = {}) => { export const styles: DiagramStylesProvider = (options: { packet?: PacketStyleOptions } = {}) => {
log.debug({ options }); log.debug({ options });
return ` return `
.byte { .byte {

View File

@@ -9,4 +9,20 @@ export type Row = Required<Block>[];
export interface PacketDB extends DiagramDB { export interface PacketDB extends DiagramDB {
getPacket: () => Row[]; getPacket: () => Row[];
getConfig: () => Required<PacketDiagramConfig>; getConfig: () => Required<PacketDiagramConfig>;
clear: () => void;
}
export interface PacketStyleOptions {
byteFontSize?: string;
startByteColor?: string;
endByteColor?: string;
labelColor?: string;
labelFontSize?: string;
blockStrokeColor?: string;
blockStrokeWidth?: string;
blockFillColor?: string;
}
export interface PacketData {
packet: Row[];
} }

View File

@@ -2,13 +2,19 @@ grammar Packet
import "../common/common"; import "../common/common";
entry Packet: entry Packet:
"packet-beta" NEWLINE* NEWLINE*
TitleAndAccessibilities? "packet-beta"
(blocks+=Block)*; (
NEWLINE* TitleAndAccessibilities blocks+=PacketBlock*
| NEWLINE+ blocks+=PacketBlock+
| NEWLINE*
)
;
Block: PacketBlock:
start=INT ('-' end=INT)? ':' label=STRING; start=INT('-' end=INT)? ':' label=STRING NEWLINE+
;
hidden terminal WS: /\s+/; hidden terminal WS: /\s+/;
terminal INT returns number: /[0-9]+/; terminal INT returns number: /0|[1-9][0-9]*/;
terminal STRING: /"[^"]*"|'[^']*'/; terminal STRING: /"[^"]*"|'[^']*'/;