дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-18 07:35:39 +02:00
Порівняти коміти
1
Коміти
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
bbdf0052b2 |
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { DocModel } from "./model/document";
|
||||
import { readOnlyQuestion, toggleReadOnly } from "./fileAttributes";
|
||||
import { offerMakeWritable, readOnlyQuestion, toggleReadOnly } from "./fileAttributes";
|
||||
|
||||
function model(readOnly: boolean, extra: Partial<{ virtual: boolean; banner: string | null }> = {}) {
|
||||
const meta = { readOnly, banner: extra.banner ?? (readOnly ? "readOnly" : null) };
|
||||
@@ -43,3 +43,27 @@ describe("Toggle Read-only Attribute", () => {
|
||||
expect(readOnlyQuestion("a.txt", false).confirm).toBe("Make Read-only");
|
||||
});
|
||||
});
|
||||
|
||||
describe("editing a read-only file", () => {
|
||||
it("offers to make it writable once, then lets edits through", async () => {
|
||||
const m = model(true);
|
||||
answer = true;
|
||||
expect(offerMakeWritable(m)).toBe(true);
|
||||
expect(offerMakeWritable(m)).toBe(true);
|
||||
expect(confirm).toHaveBeenCalledTimes(1);
|
||||
expect(confirm).toHaveBeenCalledWith(expect.objectContaining({ title: "Make File Writable" }));
|
||||
await vi.waitFor(() => expect(m.setReadOnly).toHaveBeenCalledWith(false));
|
||||
expect(offerMakeWritable(m)).toBe(false);
|
||||
});
|
||||
|
||||
it("refuses edits without asking for library, binary and too-large documents", () => {
|
||||
for (const m of [
|
||||
model(true, { virtual: true }),
|
||||
model(true, { banner: "binary" }),
|
||||
model(true, { banner: "tooLarge" }),
|
||||
])
|
||||
expect(offerMakeWritable(m)).toBe(true);
|
||||
expect(confirm).not.toHaveBeenCalled();
|
||||
expect(offerMakeWritable(model(false))).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,8 @@
|
||||
/**
|
||||
* File › File Properties › Make File Read-only / Writable (IDEA's "Toggle
|
||||
* Read-only Attribute", also the status bar lock): asks, then flips the
|
||||
* file's writable bit and the open document's editability.
|
||||
* file's writable bit and the open document's editability. Typing into a file
|
||||
* whose read-only attribute is set asks the same (IDEA's Clear Read-Only Status).
|
||||
*/
|
||||
import { call, commands, isTauri } from "@/platform/ipc";
|
||||
import { basename } from "@/platform/project";
|
||||
@@ -41,3 +42,22 @@ export function toggleReadOnly(model: DocModel) {
|
||||
(ok) => ok && void setFileWritable(model, meta.readOnly),
|
||||
);
|
||||
}
|
||||
|
||||
let asking: DocModel | null = null;
|
||||
|
||||
/**
|
||||
* An edit was attempted in `model`. When only the file's read-only attribute is in the
|
||||
* way (not a binary, too large or library file), offers to make it writable, once at a
|
||||
* time. Returns whether the edit was refused: the caller drops it either way.
|
||||
*/
|
||||
export function offerMakeWritable(model: DocModel): boolean {
|
||||
const meta = model.getMeta();
|
||||
if (!meta.readOnly) return false;
|
||||
if (model.virtual || meta.banner !== "readOnly" || asking) return true;
|
||||
asking = model;
|
||||
const q = readOnlyQuestion(basename(model.path), true);
|
||||
void confirm({ title: q.title, message: q.message, ok: q.confirm })
|
||||
.then((ok) => ok && model.getMeta().readOnly && setFileWritable(model, true))
|
||||
.finally(() => (asking = null));
|
||||
return true;
|
||||
}
|
||||
@@ -34,6 +34,7 @@ import {
|
||||
import { showContextMenu } from "@/ui";
|
||||
import { baseExtensions, compartments, contributionExtensions } from "../cm/setup";
|
||||
import { gutterMenuItems, inGutterMenuArea } from "../gutterMenu";
|
||||
import { offerMakeWritable } from "../fileAttributes";
|
||||
import type { DocModel } from "./document";
|
||||
import type { WorkspaceHistoryTarget } from "./workspaceHistory";
|
||||
import { isNavigation } from "./navHistory";
|
||||
@@ -147,6 +148,12 @@ export class EditorSlot {
|
||||
return this.state;
|
||||
}
|
||||
|
||||
private refuseReadOnlyEdit(e: Event): boolean {
|
||||
if (!offerMakeWritable(this.model)) return false;
|
||||
e.preventDefault();
|
||||
return true;
|
||||
}
|
||||
|
||||
private slotExtensions() {
|
||||
return [
|
||||
this.model.manager.workspaceHistory.extension(this.historyTarget),
|
||||
@@ -169,6 +176,10 @@ export class EditorSlot {
|
||||
this.host.onContextMenu(this, e);
|
||||
return true;
|
||||
},
|
||||
// Typing, pasting or dropping into a read-only file offers to make it writable.
|
||||
beforeinput: (e) => this.refuseReadOnlyEdit(e),
|
||||
paste: (e) => this.refuseReadOnlyEdit(e),
|
||||
drop: (e) => this.refuseReadOnlyEdit(e),
|
||||
}),
|
||||
ViewPlugin.define((view) => {
|
||||
// Gutters sit outside the content DOM. Gutter extensions with their own menu (breakpoints,
|
||||
|
||||
Reference in new issue
Заблокувати користувача