-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow to include shared styles (#41)
* feat: allow to include shared styles * feat: watch the chages for shared styles * refactor: change the injecting position of shared style
- Loading branch information
Showing
15 changed files
with
176 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import * as vscode from 'vscode' | ||
import * as path from 'path' | ||
|
||
export class Watcher extends vscode.Disposable { | ||
private sharedStyleUris: vscode.Uri[] | ||
private innerWatcher: vscode.FileSystemWatcher | ||
|
||
constructor(rootPath: string) { | ||
super(() => { | ||
this.innerWatcher.dispose() | ||
}) | ||
|
||
const config = vscode.workspace.getConfiguration('vueDesigner') | ||
|
||
this.sharedStyleUris = (config.get<string[]>('sharedStyles') || []).map( | ||
stylePath => { | ||
return vscode.Uri.file(path.join(rootPath, stylePath)) | ||
} | ||
) | ||
|
||
const pattern = new vscode.RelativePattern(rootPath, '**/*.{vue,css}') | ||
this.innerWatcher = vscode.workspace.createFileSystemWatcher(pattern) | ||
} | ||
|
||
onDidCreateComponent( | ||
fn: (uri: vscode.Uri, doc: vscode.TextDocument) => void | ||
): void { | ||
this.innerWatcher.onDidCreate(this.createVueListener(fn)) | ||
} | ||
|
||
onDidChangeComponent( | ||
fn: (uri: vscode.Uri, doc: vscode.TextDocument) => void | ||
): void { | ||
this.innerWatcher.onDidChange(this.createVueListener(fn)) | ||
} | ||
|
||
onDidDeleteComponent(fn: (uri: vscode.Uri) => void): void { | ||
this.innerWatcher.onDidDelete(fn) | ||
} | ||
|
||
onDidChangeSharedStyle(fn: (style: string) => void): void { | ||
const uris = this.sharedStyleUris.map(uri => uri.toString()) | ||
|
||
const interpolate = (uri: vscode.Uri): void => { | ||
if (uris.indexOf(uri.toString()) < 0) { | ||
return | ||
} | ||
|
||
Promise.all( | ||
this.sharedStyleUris.map(uri => { | ||
return vscode.workspace | ||
.openTextDocument(uri) | ||
.then(doc => doc.getText(), () => '') | ||
}) | ||
).then(styles => { | ||
fn(styles.join('\n')) | ||
}) | ||
} | ||
|
||
const w = this.innerWatcher | ||
w.onDidCreate(interpolate) | ||
w.onDidChange(interpolate) | ||
w.onDidDelete(interpolate) | ||
} | ||
|
||
private createVueListener( | ||
fn: (uri: vscode.Uri, doc: vscode.TextDocument) => void | ||
): (uri: vscode.Uri) => void { | ||
return uri => { | ||
if (path.extname(uri.fsPath) !== '.vue') { | ||
return | ||
} | ||
vscode.workspace.openTextDocument(uri).then(doc => { | ||
fn(uri, doc) | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.