Skip to content

Commit

Permalink
feat: expose custom blocks attrs (#3099)
Browse files Browse the repository at this point in the history
Co-authored-by: Johnson Chu <johnsoncodehk@gmail.com>
  • Loading branch information
hunyan-io and johnsoncodehk committed May 1, 2023
1 parent be0bc4d commit 34e7e7d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/vue-language-core/src/generators/script.ts
Expand Up @@ -51,6 +51,7 @@ export function generate(
endTagStart: 0,
generic: undefined,
genericOffset: 0,
attrs: {},
};
scriptSetupRanges = {
bindings: [],
Expand Down
21 changes: 18 additions & 3 deletions packages/vue-language-core/src/sourceFile.ts
Expand Up @@ -474,6 +474,7 @@ export class VueFile implements VirtualFile {
endTagStart: block.loc.end.offset,
content: block.content,
lang: block.lang ?? 'html',
attrs: block.attrs,
} : null;

if (this.sfc.template && newData) {
Expand All @@ -496,6 +497,7 @@ export class VueFile implements VirtualFile {
lang: block.lang ?? 'js',
src: block.src,
srcOffset: block.src ? this.snapshot.getText(0, block.loc.start.offset).lastIndexOf(block.src) - block.loc.start.offset : -1,
attrs: block.attrs,
} : null;

if (this.sfc.script && newData) {
Expand All @@ -518,6 +520,7 @@ export class VueFile implements VirtualFile {
lang: block.lang ?? 'js',
generic: typeof block.attrs.generic === 'string' ? block.attrs.generic : undefined,
genericOffset: typeof block.attrs.generic === 'string' ? this.snapshot.getText(0, block.loc.start.offset).lastIndexOf(block.attrs.generic) - block.loc.start.offset : -1,
attrs: block.attrs,
} : null;

if (this.sfc.scriptSetup && newData) {
Expand All @@ -542,6 +545,7 @@ export class VueFile implements VirtualFile {
lang: block.lang ?? 'css',
module: typeof block.module === 'string' ? block.module : block.module ? '$style' : undefined,
scoped: !!block.scoped,
attrs: block.attrs,
};

if (this.sfc.styles.length > i) {
Expand Down Expand Up @@ -569,6 +573,7 @@ export class VueFile implements VirtualFile {
content: block.content,
lang: block.lang ?? 'txt',
type: block.type,
attrs: block.attrs,
};

if (this.sfc.customBlocks.length > i) {
Expand All @@ -583,9 +588,19 @@ export class VueFile implements VirtualFile {
}
}

updateBlock<T>(oldBlock: T, newBlock: T) {
for (let key in newBlock) {
oldBlock[key] = newBlock[key];
updateBlock<T extends object>(oldBlock: T, newBlock: T) {
for (const key in newBlock) {
if (typeof oldBlock[key] === 'object' && typeof newBlock[key] === 'object') {
this.updateBlock(oldBlock[key] as object, newBlock[key] as object);
}
else {
oldBlock[key] = newBlock[key];
}
}
for (const key in oldBlock) {
if (!(key in newBlock)) {
delete oldBlock[key];
}
}
}
}
1 change: 1 addition & 0 deletions packages/vue-language-core/src/types.ts
Expand Up @@ -72,6 +72,7 @@ export interface SfcBlock {
endTagStart: number;
lang: string;
content: string;
attrs: Record<string, string | true>;
}

export interface Sfc {
Expand Down

0 comments on commit 34e7e7d

Please sign in to comment.