mirror of
https://github.com/zen-browser/desktop.git
synced 2026-03-06 08:47:03 +00:00
This commit addresses an issue where edited pinned tab titles were not consistently updated across all windows and in storage. The following changes were made: - **ZenUIManager.mjs:** Removed redundant `pinId` variable and directly checked `zen-pin-id` attribute. - **ZenPinnedTabsStorage.mjs:** Added `edited_title` column to the `zen_pins` table to track if a pin's title has been manually edited. Also, the `updatePinTitle` function now accepts an `isEdited` flag (defaults to true) and updates the `edited_title` column accordingly. - **ZenPinnedTabManager.mjs:** The `updatePinTitle` function was added to handle the update of pin titles. It updates the title in storage using `ZenPinnedTabsStorage.updatePinTitle`, refreshes the pinned tabs, and then iterates through all browser windows to update the labels of corresponding pinned tabs. Also, a check was added to `_initializePinnedTabs` to set the `zen-has-static-label` attribute on tabs with `editedTitle` set to `true`. - **ZenWorkspaces.mjs:** Added a null check for `activeWorkspaceStrip` to avoid errors when it's not available. These changes ensure that when a pinned tab title is edited, the updated title is correctly persisted in storage and reflected across all open windows. The `edited_title` flag allows distinguishing between default titles and custom ones.
435 lines
13 KiB
JavaScript
435 lines
13 KiB
JavaScript
var ZenPinnedTabsStorage = {
|
|
async init() {
|
|
await this._ensureTable();
|
|
},
|
|
|
|
async _ensureTable() {
|
|
await PlacesUtils.withConnectionWrapper('ZenPinnedTabsStorage._ensureTable', async (db) => {
|
|
// Create the pins table if it doesn't exist
|
|
await db.execute(`
|
|
CREATE TABLE IF NOT EXISTS zen_pins (
|
|
id INTEGER PRIMARY KEY,
|
|
uuid TEXT UNIQUE NOT NULL,
|
|
title TEXT NOT NULL,
|
|
url TEXT,
|
|
container_id INTEGER,
|
|
workspace_uuid TEXT,
|
|
position INTEGER NOT NULL DEFAULT 0,
|
|
is_essential BOOLEAN NOT NULL DEFAULT 0,
|
|
is_group BOOLEAN NOT NULL DEFAULT 0,
|
|
parent_uuid TEXT,
|
|
created_at INTEGER NOT NULL,
|
|
updated_at INTEGER NOT NULL,
|
|
FOREIGN KEY (parent_uuid) REFERENCES zen_pins(uuid) ON DELETE SET NULL
|
|
)
|
|
`);
|
|
|
|
const columns = await db.execute(`PRAGMA table_info(zen_pins)`);
|
|
const columnNames = columns.map((row) => row.getResultByName('name'));
|
|
|
|
// Helper function to add column if it doesn't exist
|
|
const addColumnIfNotExists = async (columnName, definition) => {
|
|
if (!columnNames.includes(columnName)) {
|
|
await db.execute(`ALTER TABLE zen_pins ADD COLUMN ${columnName} ${definition}`);
|
|
}
|
|
};
|
|
|
|
// Add edited_title column if it doesn't exist
|
|
await addColumnIfNotExists('edited_title', 'BOOLEAN NOT NULL DEFAULT 0');
|
|
|
|
// Create indices
|
|
await db.execute(`
|
|
CREATE INDEX IF NOT EXISTS idx_zen_pins_uuid ON zen_pins(uuid)
|
|
`);
|
|
|
|
await db.execute(`
|
|
CREATE INDEX IF NOT EXISTS idx_zen_pins_parent_uuid ON zen_pins(parent_uuid)
|
|
`);
|
|
|
|
// Create the changes tracking table if it doesn't exist
|
|
await db.execute(`
|
|
CREATE TABLE IF NOT EXISTS zen_pins_changes (
|
|
uuid TEXT PRIMARY KEY,
|
|
timestamp INTEGER NOT NULL
|
|
)
|
|
`);
|
|
|
|
// Create an index on the uuid column for changes tracking table
|
|
await db.execute(`
|
|
CREATE INDEX IF NOT EXISTS idx_zen_pins_changes_uuid ON zen_pins_changes(uuid)
|
|
`);
|
|
|
|
this._resolveInitialized();
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Private helper method to notify observers with a list of changed UUIDs.
|
|
* @param {string} event - The observer event name.
|
|
* @param {Array<string>} uuids - Array of changed workspace UUIDs.
|
|
*/
|
|
_notifyPinsChanged(event, uuids) {
|
|
if (uuids.length === 0) return; // No changes to notify
|
|
|
|
// Convert the array of UUIDs to a JSON string
|
|
const data = JSON.stringify(uuids);
|
|
|
|
Services.obs.notifyObservers(null, event, data);
|
|
},
|
|
|
|
async savePin(pin, notifyObservers = true) {
|
|
const changedUUIDs = new Set();
|
|
|
|
await PlacesUtils.withConnectionWrapper('ZenPinnedTabsStorage.savePin', async (db) => {
|
|
await db.executeTransaction(async () => {
|
|
const now = Date.now();
|
|
|
|
let newPosition;
|
|
if ('position' in pin && Number.isFinite(pin.position)) {
|
|
newPosition = pin.position;
|
|
} else {
|
|
// Get the maximum position within the same parent group (or null for root level)
|
|
const maxPositionResult = await db.execute(
|
|
`
|
|
SELECT MAX("position") as max_position
|
|
FROM zen_pins
|
|
WHERE COALESCE(parent_uuid, '') = COALESCE(:parent_uuid, '')
|
|
`,
|
|
{ parent_uuid: pin.parentUuid || null }
|
|
);
|
|
const maxPosition = maxPositionResult[0].getResultByName('max_position') || 0;
|
|
newPosition = maxPosition + 1000;
|
|
}
|
|
|
|
// Insert or replace the pin
|
|
await db.executeCached(
|
|
`
|
|
INSERT OR REPLACE INTO zen_pins (
|
|
uuid, title, url, container_id, workspace_uuid, position,
|
|
is_essential, is_group, parent_uuid, created_at, updated_at
|
|
) VALUES (
|
|
:uuid, :title, :url, :container_id, :workspace_uuid, :position,
|
|
:is_essential, :is_group, :parent_uuid,
|
|
COALESCE((SELECT created_at FROM zen_pins WHERE uuid = :uuid), :now),
|
|
:now
|
|
)
|
|
`,
|
|
{
|
|
uuid: pin.uuid,
|
|
title: pin.title,
|
|
url: pin.isGroup ? null : pin.url,
|
|
container_id: pin.containerTabId || null,
|
|
workspace_uuid: pin.workspaceUuid || null,
|
|
position: newPosition,
|
|
is_essential: pin.isEssential || false,
|
|
is_group: pin.isGroup || false,
|
|
parent_uuid: pin.parentUuid || null,
|
|
now,
|
|
}
|
|
);
|
|
|
|
await db.execute(
|
|
`
|
|
INSERT OR REPLACE INTO zen_pins_changes (uuid, timestamp)
|
|
VALUES (:uuid, :timestamp)
|
|
`,
|
|
{
|
|
uuid: pin.uuid,
|
|
timestamp: Math.floor(now / 1000),
|
|
}
|
|
);
|
|
|
|
changedUUIDs.add(pin.uuid);
|
|
await this.updateLastChangeTimestamp(db);
|
|
});
|
|
});
|
|
|
|
if (notifyObservers) {
|
|
this._notifyPinsChanged('zen-pin-updated', Array.from(changedUUIDs));
|
|
}
|
|
},
|
|
|
|
async getPins() {
|
|
const db = await PlacesUtils.promiseDBConnection();
|
|
const rows = await db.executeCached(`
|
|
SELECT * FROM zen_pins
|
|
ORDER BY parent_uuid NULLS FIRST, position ASC
|
|
`);
|
|
return rows.map((row) => ({
|
|
uuid: row.getResultByName('uuid'),
|
|
title: row.getResultByName('title'),
|
|
url: row.getResultByName('url'),
|
|
containerTabId: row.getResultByName('container_id'),
|
|
workspaceUuid: row.getResultByName('workspace_uuid'),
|
|
position: row.getResultByName('position'),
|
|
isEssential: Boolean(row.getResultByName('is_essential')),
|
|
isGroup: Boolean(row.getResultByName('is_group')),
|
|
parentUuid: row.getResultByName('parent_uuid'),
|
|
editedTitle: Boolean(row.getResultByName('edited_title')),
|
|
}));
|
|
},
|
|
|
|
async getGroupChildren(groupUuid) {
|
|
const db = await PlacesUtils.promiseDBConnection();
|
|
const rows = await db.executeCached(
|
|
`
|
|
SELECT * FROM zen_pins
|
|
WHERE parent_uuid = :groupUuid
|
|
ORDER BY position ASC
|
|
`,
|
|
{ groupUuid }
|
|
);
|
|
|
|
return rows.map((row) => ({
|
|
uuid: row.getResultByName('uuid'),
|
|
title: row.getResultByName('title'),
|
|
url: row.getResultByName('url'),
|
|
containerTabId: row.getResultByName('container_id'),
|
|
workspaceUuid: row.getResultByName('workspace_uuid'),
|
|
position: row.getResultByName('position'),
|
|
isEssential: Boolean(row.getResultByName('is_essential')),
|
|
isGroup: Boolean(row.getResultByName('is_group')),
|
|
parentUuid: row.getResultByName('parent_uuid'),
|
|
editedTitle: Boolean(row.getResultByName('edited_title')),
|
|
}));
|
|
},
|
|
|
|
async removePin(uuid, notifyObservers = true) {
|
|
const changedUUIDs = [uuid];
|
|
|
|
await PlacesUtils.withConnectionWrapper('ZenPinnedTabsStorage.removePin', async (db) => {
|
|
await db.executeTransaction(async () => {
|
|
// Get all child UUIDs first for change tracking
|
|
const children = await db.execute(`SELECT uuid FROM zen_pins WHERE parent_uuid = :uuid`, { uuid });
|
|
|
|
// Add child UUIDs to changedUUIDs array
|
|
for (const child of children) {
|
|
changedUUIDs.push(child.getResultByName('uuid'));
|
|
}
|
|
|
|
// Delete all children in a single statement
|
|
await db.execute(`DELETE FROM zen_pins WHERE parent_uuid = :uuid`, { uuid });
|
|
|
|
// Delete the pin/group itself
|
|
await db.execute(`DELETE FROM zen_pins WHERE uuid = :uuid`, { uuid });
|
|
|
|
// Record the changes
|
|
const now = Math.floor(Date.now() / 1000);
|
|
for (const changedUuid of changedUUIDs) {
|
|
await db.execute(
|
|
`
|
|
INSERT OR REPLACE INTO zen_pins_changes (uuid, timestamp)
|
|
VALUES (:uuid, :timestamp)
|
|
`,
|
|
{
|
|
uuid: changedUuid,
|
|
timestamp: now,
|
|
}
|
|
);
|
|
}
|
|
|
|
await this.updateLastChangeTimestamp(db);
|
|
});
|
|
});
|
|
|
|
if (notifyObservers) {
|
|
this._notifyPinsChanged('zen-pin-removed', changedUUIDs);
|
|
}
|
|
},
|
|
|
|
async wipeAllPins() {
|
|
await PlacesUtils.withConnectionWrapper('ZenPinnedTabsStorage.wipeAllPins', async (db) => {
|
|
await db.execute(`DELETE FROM zen_pins`);
|
|
await db.execute(`DELETE FROM zen_pins_changes`);
|
|
await this.updateLastChangeTimestamp(db);
|
|
});
|
|
},
|
|
|
|
async markChanged(uuid) {
|
|
await PlacesUtils.withConnectionWrapper('ZenPinnedTabsStorage.markChanged', async (db) => {
|
|
const now = Date.now();
|
|
await db.execute(
|
|
`
|
|
INSERT OR REPLACE INTO zen_pins_changes (uuid, timestamp)
|
|
VALUES (:uuid, :timestamp)
|
|
`,
|
|
{
|
|
uuid,
|
|
timestamp: Math.floor(now / 1000),
|
|
}
|
|
);
|
|
});
|
|
},
|
|
|
|
async getChangedIDs() {
|
|
const db = await PlacesUtils.promiseDBConnection();
|
|
const rows = await db.execute(`
|
|
SELECT uuid, timestamp FROM zen_pins_changes
|
|
`);
|
|
const changes = {};
|
|
for (const row of rows) {
|
|
changes[row.getResultByName('uuid')] = row.getResultByName('timestamp');
|
|
}
|
|
return changes;
|
|
},
|
|
|
|
async clearChangedIDs() {
|
|
await PlacesUtils.withConnectionWrapper('ZenPinnedTabsStorage.clearChangedIDs', async (db) => {
|
|
await db.execute(`DELETE FROM zen_pins_changes`);
|
|
});
|
|
},
|
|
|
|
shouldReorderPins(before, current, after) {
|
|
const minGap = 1; // Minimum allowed gap between positions
|
|
return (before !== null && current - before < minGap) || (after !== null && after - current < minGap);
|
|
},
|
|
|
|
async reorderAllPins(db, changedUUIDs) {
|
|
const pins = await db.execute(`
|
|
SELECT uuid
|
|
FROM zen_pins
|
|
ORDER BY position ASC
|
|
`);
|
|
|
|
for (let i = 0; i < pins.length; i++) {
|
|
const newPosition = (i + 1) * 1000; // Use large increments
|
|
await db.execute(
|
|
`
|
|
UPDATE zen_pins
|
|
SET position = :newPosition
|
|
WHERE uuid = :uuid
|
|
`,
|
|
{ newPosition, uuid: pins[i].getResultByName('uuid') }
|
|
);
|
|
changedUUIDs.add(pins[i].getResultByName('uuid'));
|
|
}
|
|
},
|
|
|
|
async updateLastChangeTimestamp(db) {
|
|
const now = Date.now();
|
|
await db.execute(
|
|
`
|
|
INSERT OR REPLACE INTO moz_meta (key, value)
|
|
VALUES ('zen_pins_last_change', :now)
|
|
`,
|
|
{ now }
|
|
);
|
|
},
|
|
|
|
async getLastChangeTimestamp() {
|
|
const db = await PlacesUtils.promiseDBConnection();
|
|
const result = await db.executeCached(`
|
|
SELECT value FROM moz_meta WHERE key = 'zen_pins_last_change'
|
|
`);
|
|
return result.length ? parseInt(result[0].getResultByName('value'), 10) : 0;
|
|
},
|
|
|
|
async updatePinPositions(pins) {
|
|
const changedUUIDs = new Set();
|
|
|
|
await PlacesUtils.withConnectionWrapper('ZenPinnedTabsStorage.updatePinPositions', async (db) => {
|
|
await db.executeTransaction(async () => {
|
|
const now = Date.now();
|
|
|
|
for (let i = 0; i < pins.length; i++) {
|
|
const pin = pins[i];
|
|
const newPosition = (i + 1) * 1000;
|
|
|
|
await db.execute(
|
|
`
|
|
UPDATE zen_pins
|
|
SET position = :newPosition
|
|
WHERE uuid = :uuid
|
|
`,
|
|
{ newPosition, uuid: pin.uuid }
|
|
);
|
|
|
|
changedUUIDs.add(pin.uuid);
|
|
|
|
// Record the change
|
|
await db.execute(
|
|
`
|
|
INSERT OR REPLACE INTO zen_pins_changes (uuid, timestamp)
|
|
VALUES (:uuid, :timestamp)
|
|
`,
|
|
{
|
|
uuid: pin.uuid,
|
|
timestamp: Math.floor(now / 1000),
|
|
}
|
|
);
|
|
}
|
|
|
|
await this.updateLastChangeTimestamp(db);
|
|
});
|
|
});
|
|
|
|
this._notifyPinsChanged('zen-pin-updated', Array.from(changedUUIDs));
|
|
},
|
|
|
|
async updatePinTitle(uuid, newTitle, isEdited = true, notifyObservers = true) {
|
|
if (!uuid || typeof newTitle !== 'string') {
|
|
throw new Error('Invalid parameters: uuid and newTitle are required');
|
|
}
|
|
|
|
const changedUUIDs = new Set();
|
|
|
|
await PlacesUtils.withConnectionWrapper('ZenPinnedTabsStorage.updatePinTitle', async (db) => {
|
|
await db.executeTransaction(async () => {
|
|
const now = Date.now();
|
|
|
|
// Update the pin's title and edited_title flag
|
|
const result = await db.execute(
|
|
`
|
|
UPDATE zen_pins
|
|
SET title = :newTitle,
|
|
edited_title = :isEdited,
|
|
updated_at = :now
|
|
WHERE uuid = :uuid
|
|
`,
|
|
{
|
|
uuid,
|
|
newTitle,
|
|
isEdited,
|
|
now,
|
|
}
|
|
);
|
|
|
|
// Only proceed with change tracking if a row was actually updated
|
|
if (result.rowsAffected > 0) {
|
|
changedUUIDs.add(uuid);
|
|
|
|
// Record the change
|
|
await db.execute(
|
|
`
|
|
INSERT OR REPLACE INTO zen_pins_changes (uuid, timestamp)
|
|
VALUES (:uuid, :timestamp)
|
|
`,
|
|
{
|
|
uuid,
|
|
timestamp: Math.floor(now / 1000),
|
|
}
|
|
);
|
|
|
|
await this.updateLastChangeTimestamp(db);
|
|
}
|
|
});
|
|
});
|
|
|
|
if (notifyObservers && changedUUIDs.size > 0) {
|
|
this._notifyPinsChanged('zen-pin-updated', Array.from(changedUUIDs));
|
|
}
|
|
},
|
|
|
|
async __dropTables() {
|
|
await PlacesUtils.withConnectionWrapper('ZenPinnedTabsStorage.__dropTables', async (db) => {
|
|
await db.execute(`DROP TABLE IF EXISTS zen_pins`);
|
|
await db.execute(`DROP TABLE IF EXISTS zen_pins_changes`);
|
|
});
|
|
},
|
|
};
|
|
|
|
ZenPinnedTabsStorage.promiseInitialized = new Promise((resolve) => {
|
|
ZenPinnedTabsStorage._resolveInitialized = resolve;
|
|
ZenPinnedTabsStorage.init();
|
|
});
|