-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from lumpinif/main
ref: bump to v 3.1.3
- Loading branch information
Showing
10 changed files
with
235 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,6 @@ | ||
import "@/styles/globals.css" | ||
|
||
import { shouldInitialize } from "@/utils/url-utils" | ||
import { ExtensionManager } from "./v3/managers/extension-manager" | ||
|
||
import { addThinkingBlockToggle } from "./v3/features/thinking-block" | ||
|
||
// Only initialize on appropriate pages | ||
if (shouldInitialize(window.location.href)) { | ||
if (document.readyState === "loading") { | ||
document.addEventListener("DOMContentLoaded", addThinkingBlockToggle) | ||
} else { | ||
addThinkingBlockToggle() | ||
} | ||
} | ||
const extensionManager = new ExtensionManager() | ||
extensionManager.initialize() |
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,28 @@ | ||
import { Feature } from "@/types" | ||
|
||
/** | ||
* Base abstract class for features | ||
* Provides common functionality and enforces feature contract | ||
*/ | ||
export abstract class BaseFeature implements Feature { | ||
constructor(readonly id: string) {} | ||
|
||
/** | ||
* Initialize the feature | ||
* @returns cleanup function if needed | ||
*/ | ||
abstract initialize(): void | (() => void) | ||
|
||
/** | ||
* Helper method to safely add event listeners with automatic cleanup | ||
*/ | ||
protected addEventListenerWithCleanup( | ||
element: Element, | ||
event: string, | ||
handler: EventListener, | ||
options?: boolean | AddEventListenerOptions | ||
): () => void { | ||
element.addEventListener(event, handler, options) | ||
return () => element.removeEventListener(event, handler, options) | ||
} | ||
} |
34 changes: 33 additions & 1 deletion
34
extensions/chrome/src/content/v3/features/thinking-block/index.ts
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 |
---|---|---|
@@ -1 +1,33 @@ | ||
export { addThinkingBlockToggle } from "./thinking-block-toggle" | ||
import type { MutationObserverService } from "@/services/mutation-observer" | ||
|
||
import { BaseFeature } from "../base-feature" | ||
import { processThinkingBlocks } from "./process-thinking-block" | ||
|
||
/** | ||
* Feature that adds toggle functionality to thinking blocks in the UI | ||
* Manages the collapse/expand and copy functionality for code blocks | ||
*/ | ||
export class TCThinkingBlock extends BaseFeature { | ||
/** | ||
* @param mutationObserver - Service to observe DOM changes for thinking blocks | ||
*/ | ||
constructor(private mutationObserver: MutationObserverService) { | ||
super("tc-thinking-block") | ||
} | ||
|
||
/** | ||
* Initialize the thinking block feature | ||
* Sets up mutation observer to watch for new thinking blocks | ||
* @returns Cleanup function to unsubscribe from mutation observer | ||
*/ | ||
initialize(): void | (() => void) { | ||
this.mutationObserver.initialize() | ||
|
||
const unsubscribe = this.mutationObserver.subscribe(processThinkingBlocks) | ||
|
||
return () => { | ||
// Unsubscribe from mutation observer | ||
unsubscribe() | ||
} | ||
} | ||
} |
8 changes: 1 addition & 7 deletions
8
...s/thinking-block/thinking-block-toggle.ts → .../thinking-block/process-thinking-block.ts
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
65 changes: 65 additions & 0 deletions
65
extensions/chrome/src/content/v3/managers/extension-manager.ts
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,65 @@ | ||
import { MutationObserverService } from "@/services/mutation-observer" | ||
import { shouldInitialize } from "@/utils/url-utils" | ||
|
||
import { TCThinkingBlock } from "../features/thinking-block" | ||
import { FeatureManager } from "./feature-manager" | ||
|
||
/** | ||
* Manages the lifecycle and coordination of all extension features and services | ||
*/ | ||
export class ExtensionManager { | ||
private featureManager: FeatureManager | ||
private mutationObserver: MutationObserverService | ||
|
||
constructor() { | ||
this.mutationObserver = new MutationObserverService() | ||
this.featureManager = new FeatureManager() | ||
|
||
this.registerFeatures() | ||
this.setupNavigationListener() | ||
} | ||
|
||
/** | ||
* Register all extension features | ||
*/ | ||
private registerFeatures(): void { | ||
// Register features with their required services | ||
this.featureManager.register(new TCThinkingBlock(this.mutationObserver)) | ||
// Add more features here | ||
} | ||
|
||
/** | ||
* Initialize the extension if conditions are met | ||
*/ | ||
initialize(): void { | ||
if (!shouldInitialize(window.location.href)) { | ||
return | ||
} | ||
|
||
if (document.readyState === "loading") { | ||
document.addEventListener("DOMContentLoaded", () => { | ||
this.featureManager.initialize() | ||
}) | ||
} else { | ||
this.featureManager.initialize() | ||
} | ||
} | ||
|
||
/** | ||
* Set up listener for navigation events | ||
*/ | ||
private setupNavigationListener(): void { | ||
chrome.runtime.onMessage.addListener((message) => { | ||
if (message.type === "NAVIGATION") { | ||
this.featureManager.cleanup() | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Clean up all features and services | ||
*/ | ||
cleanup(): void { | ||
this.featureManager.cleanup() | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
extensions/chrome/src/content/v3/managers/feature-manager.ts
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,56 @@ | ||
import { Feature } from "@/types" | ||
|
||
export class FeatureManager { | ||
private features = new Map<string, Feature>() | ||
private cleanupFunctions = new Map<string, () => void>() | ||
|
||
/** | ||
* Register a new feature | ||
* @param feature Feature instance to register | ||
* @throws Error if feature with same id already exists | ||
*/ | ||
register(feature: Feature): void { | ||
if (this.features.has(feature.id)) { | ||
throw new Error(`Feature with id ${feature.id} already exists`) | ||
} | ||
this.features.set(feature.id, feature) | ||
} | ||
|
||
/** | ||
* Initialize all registered features | ||
*/ | ||
initialize(): void { | ||
this.features.forEach((feature, id) => { | ||
try { | ||
const cleanup = feature.initialize() | ||
if (cleanup) { | ||
this.cleanupFunctions.set(id, cleanup) | ||
} | ||
} catch (error) { | ||
console.error(`Failed to initialize feature ${id}:`, error) | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Clean up all features | ||
*/ | ||
cleanup(): void { | ||
this.cleanupFunctions.forEach((cleanup, id) => { | ||
try { | ||
cleanup() | ||
} catch (error) { | ||
console.error(`Failed to cleanup feature ${id}:`, error) | ||
} | ||
}) | ||
this.cleanupFunctions.clear() | ||
this.features.clear() | ||
} | ||
|
||
/** | ||
* Get a registered feature by id | ||
*/ | ||
getFeature(id: string): Feature | undefined { | ||
return this.features.get(id) | ||
} | ||
} |
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,7 @@ | ||
/** | ||
* Base interface for all features | ||
*/ | ||
export interface Feature { | ||
id: string | ||
initialize(): void | (() => void) // Return cleanup function if needed | ||
} |