Skip to content

Commit

Permalink
Merge pull request #109 from stneveadomi/103-store-rules-in-settingsjson
Browse files Browse the repository at this point in the history
Solution for Import / Export of rules
  • Loading branch information
stneveadomi authored Sep 27, 2024
2 parents 83ca885 + 6ceadee commit 59e3b00
Show file tree
Hide file tree
Showing 16 changed files with 272 additions and 85 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Introduced 3 new minimize commands: All, local, and global.
- Added debug setting for showing rule states.
- Introduce before and after decoration states with corresponding UX.
- Introduced new webview actions:
- **Minimize all rules**
- **Edit Mode. Raw editing of the rules.**
- **Export rules to clipboard**

### Changed

- Addressed bug where disabled rules would apply if editing document.
- Fixed UX bug with occurrence drop down.
- Fixed broken license badge in release notes view.

### Removed

Expand Down
30 changes: 30 additions & 0 deletions ext-src/commands/commandManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,36 @@ export class CommandManager {
},
this.logger,
),
new Command(
'grepc.editRulesLocal',
() => {
this.rfm.getRuleFactory(LocationState.LOCAL)?.pushEditRules();
},
this.logger,
),
new Command(
'grepc.editRulesGlobal',
() => {
this.rfm.getRuleFactory(LocationState.GLOBAL)?.pushEditRules();
},
this.logger,
),
new Command(
'grepc.exportRulesLocal',
() => {
vscode.env.clipboard.writeText(JSON.stringify(this.rfm.getRuleFactory(LocationState.LOCAL)?.getRulesArray()));
vscode.window.showInformationMessage('✅ Copied local rules to clipboard.');
},
this.logger,
),
new Command(
'grepc.exportRulesGlobal',
() => {
vscode.env.clipboard.writeText(JSON.stringify(this.rfm.getRuleFactory(LocationState.GLOBAL)?.getRulesArray()));
vscode.window.showInformationMessage('✅ Copied global rules to clipboard.');
},
this.logger,
),
];
}

Expand Down
9 changes: 9 additions & 0 deletions ext-src/rules/ruleFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class RuleFactory {
private localState: vscode.Memento | undefined = undefined;
private globalState: GlobalState | undefined = undefined;
private _enabledRules = new Subject<Rule[]>();
private editMode = false;

private _grepcProvider: GrepcViewProvider | undefined = undefined;
private static RULES_MAP_KEY_ID = 'rulesMap';
Expand Down Expand Up @@ -309,6 +310,14 @@ export class RuleFactory {
});
}

pushEditRules() {
this.editMode = !this.editMode;
this._grepcProvider?.webview?.postMessage({
type: 'editMode',
value: this.editMode,
});
}

/**
* recast the current enabled rules for use in triggerUpdateDecorations.
* For more info, see DecorationTypeManager::enableDecorationDetection
Expand Down
2 changes: 1 addition & 1 deletion ext-src/viewProviders/releaseNotesViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class ReleaseNotesWebview {
<div>
<p dir="auto">
<a href="https://github.com/stneveadomi/grepc/">
<img nonce="${nonce}" src="https://camo.githubusercontent.com/e51b657236415672754f02dfef0bc6873e979346fa0107f9c4219fe1589a5c6c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265" alt="GitHub license" data-canonical-src="https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square" style="max-width: 100%;">
<img nonce="${nonce}" src="https://img.shields.io/github/license/stneveadomi/grepc">
</a>
<a href="https://github.com/stneveadomi/grepc/releases">
<img alt="GitHub package.json version" nonce="${nonce}" src="https://img.shields.io/github/package-json/v/stneveadomi/grepc?color=limegreen">
Expand Down
60 changes: 57 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,33 @@
},
{
"command": "grepc.minimizeLocal",
"title": "grepc: Minimize Local"
"title": "grepc: Minimize Local",
"icon": "$(collapse-all)"
},
{
"command": "grepc.minimizeGlobal",
"title": "grepc: Minimize Global"
"title": "grepc: Minimize Global",
"icon": "$(collapse-all)"
},
{
"command": "grepc.editRulesLocal",
"title": "Edit Rules",
"icon": "$(code)"
},
{
"command": "grepc.editRulesGlobal",
"title": "Edit Rules",
"icon": "$(code)"
},
{
"command": "grepc.exportRulesLocal",
"title": "Export Rules to Clipboard",
"icon": "$(export)"
},
{
"command": "grepc.exportRulesGlobal",
"title": "Export Rules to Clipboard",
"icon": "$(export)"
}
],
"menus": {
Expand All @@ -168,6 +190,38 @@
"group": "grepc",
"when": "editorHasSelection"
}
],
"view/title": [
{
"command": "grepc.minimizeLocal",
"group": "navigation",
"when": "view == grepc.webview.local"
},
{
"command": "grepc.minimizeGlobal",
"group": "navigation",
"when": "view == grepc.webview.global"
},
{
"command": "grepc.editRulesLocal",
"group": "navigation",
"when": "view == grepc.webview.local"
},
{
"command": "grepc.editRulesGlobal",
"group": "navigation",
"when": "view == grepc.webview.global"
},
{
"command": "grepc.exportRulesLocal",
"group": "navigation",
"when": "view == grepc.webview.local"
},
{
"command": "grepc.exportRulesGlobal",
"group": "navigation",
"when": "view == grepc.webview.global"
}
]
},
"views": {
Expand Down Expand Up @@ -245,6 +299,6 @@
},
"lint-staged": {
"*.{ts,js}": "eslint ./ext-src/ --fix",
"*.{ts,js,css,md,json}": "prettier . --write"
"*.{html,ts,js,css,md,json}": "prettier . --write"
}
}
2 changes: 1 addition & 1 deletion webview-ui/grepc-webview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@
},
"lint-staged": {
"*.{ts,js}": "eslint --fix",
"*.{ts,js,css,md}": "prettier --write"
"*.{html,ts,js,css,md}": "prettier --write"
}
}
6 changes: 5 additions & 1 deletion webview-ui/grepc-webview/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<div class="main-container">
<div *ngIf="!extension.editMode" class="main-container">
<app-rule *ngFor="let r of $rules | async" [rule]="r"> </app-rule>
<app-add-rule> </app-add-rule>
</div>

<div class="edit-mode-container" *ngIf="extension.editMode">
<app-edit-mode> </app-edit-mode>
</div>

<div #dropOverlay class="drop-overlay" *ngIf="showDropOverlay()"></div>
14 changes: 12 additions & 2 deletions webview-ui/grepc-webview/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ import { ExtensionService } from '../services/extension.service';
import { GlobalStylesService } from '../services/global-styles.service';
import { DragService } from '../services/drag.service';
import { LoggerService } from '../services/logger.service';
import { EditModeComponent } from './edit-mode/edit-mode.component';

@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, RouterOutlet, RuleComponent, AddRuleComponent],
imports: [
CommonModule,
RouterOutlet,
RuleComponent,
AddRuleComponent,
EditModeComponent,
],
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
Expand All @@ -34,7 +41,7 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy {

constructor(
protected ruleService: RuleService,
protected extension: ExtensionService,
public extension: ExtensionService,
protected globalStylesService: GlobalStylesService,
protected dragService: DragService,
protected logger: LoggerService,
Expand Down Expand Up @@ -117,6 +124,9 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
case 'debugMode':
this.extension.debugMode = event.data?.value;
break;
case 'editMode':
this.extension.editMode = event.data?.value;
break;
}
}
}
13 changes: 13 additions & 0 deletions webview-ui/grepc-webview/src/app/edit-mode/edit-mode.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.edit-block {
height: 100%;
width: 80%;
display: inline-block;
padding: 1em;
margin: 1em;
overflow-x: hidden;
white-space: pre-wrap;
}

.invalid {
border: 1px solid red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<pre
class="edit-block"
[class.invalid]="!isValid"
(blur)="onBlur($event.target)"
tabindex="0"
contenteditable
>{{ ruleArray }}
</pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { EditModeComponent } from './edit-mode.component';

describe('EditModeComponent', () => {
let component: EditModeComponent;
let fixture: ComponentFixture<EditModeComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [EditModeComponent],
}).compileComponents();

fixture = TestBed.createComponent(EditModeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
37 changes: 37 additions & 0 deletions webview-ui/grepc-webview/src/app/edit-mode/edit-mode.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Component, OnInit } from '@angular/core';
import { RuleService } from '../../services/rule.service';

@Component({
selector: 'app-edit-mode',
standalone: true,
imports: [],
templateUrl: './edit-mode.component.html',
styleUrl: './edit-mode.component.css',
})
export class EditModeComponent implements OnInit {
ruleArray = '';
isValid = true;

constructor(protected ruleService: RuleService) {}

ngOnInit() {
this.ruleArray = JSON.stringify(
this.ruleService.getRuleArray(),
undefined,
' ',
).trim();
}

onBlur(rules: EventTarget | null) {
if (rules instanceof HTMLElement) {
const htmlEvent = rules as HTMLElement;
if (
(this.isValid = this.ruleService.areValidRules(
htmlEvent.innerHTML,
))
) {
this.ruleService.pushNewRuleArray(htmlEvent.innerHTML);
}
}
}
}
Loading

0 comments on commit 59e3b00

Please sign in to comment.