{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "debugging-docs",
  "title": "Debugging",
  "description": "Debugging in Plate.",
  "files": [
    {
      "path": "../../content/docs/(guides)/debugging.mdx",
      "content": "---\ntitle: Debugging\ndescription: Debugging in Plate.\n---\n\n## Using the DebugPlugin\n\nThe `DebugPlugin` is automatically included when you create a Plate editor. You can access its methods through the editor's API:\n\n```ts\nconst editor = createPlateEditor({\n  plugins: [/* your plugins */],\n});\n\neditor.api.debug.log('This is a log message');\neditor.api.debug.info('This is an info message');\neditor.api.debug.warn('This is a warning');\neditor.api.debug.error('This is an error');\n```\n\n### Log Levels\n\nThe `DebugPlugin` supports four log levels:\n\n1. `log`: For general logging\n2. `info`: For informational messages\n3. `warn`: For warnings\n4. `error`: For errors\n\nYou can set the minimum log level to control which messages are displayed:\n\n```ts\nconst editor = createPlateEditor({\n  plugins: [\n    DebugPlugin.configure({\n      options: {\n        logLevel: 'warn', // Only show warnings and errors\n      },\n    }),\n  ],\n});\n```\n\n### Configuration Options\n\nThe `DebugPlugin` can be configured with the following options:\n\n- `isProduction`: Set to `true` to disable logging in production environments.\n- `logLevel`: Set the minimum log level (`'error'`, `'warn'`, `'info'`, or `'log'`).\n- `logger`: Provide custom logging functions for each log level.\n- `throwErrors`: Set to `true` to throw errors instead of logging them (default: `true`).\n\nExample configuration:\n\n```ts\nconst editor = createPlateEditor({\n  plugins: [\n    DebugPlugin.configure({\n      options: {\n        isProduction: process.env.NODE_ENV === 'production',\n        logLevel: 'info',\n        logger: {\n          error: (message, type, details) => {\n            // Custom error logging\n            console.error(`Custom Error: ${message}`, type, details);\n          },\n          // ... custom loggers for other levels\n        },\n        throwErrors: false,\n      },\n    }),\n  ],\n});\n```\n\n### Error Handling\n\nBy default, the `DebugPlugin` throws errors when `error` is called. You can catch these errors and handle them as needed:\n\n```ts\ntry {\n  editor.api.debug.error('An error occurred', 'CUSTOM_ERROR', { details: 'Additional information' });\n} catch (error) {\n  if (error instanceof PlateError) {\n    console.debug(error.type); // 'CUSTOM_ERROR'\n    console.debug(error.message); // '[CUSTOM_ERROR] An error occurred'\n  }\n}\n```\n\nTo log errors instead of throwing them, set `throwErrors` to `false` in the configuration.\n\n### Best Practices\n\n1. Use appropriate log levels for different types of messages.\n2. In production, set `isProduction` to `true` to disable non-essential logging.\n3. Use custom loggers to integrate with your preferred logging service.\n4. Include relevant details when logging to make debugging easier.\n5. Use error types to categorize and handle different error scenarios.\n\n## Additional Debugging Strategies\n\nBesides using the DebugPlugin, there are other effective ways to debug your Plate editor:\n\n### 1. Override Editor Methods with Logging\n\nYou can use the `extendEditor` option to override editor methods and add logging:\n\n```ts\nconst LoggingPlugin = createPlatePlugin({\n  key: 'logging',\n}).overrideEditor(({ editor, tf: { apply } }) => ({\n  transforms: {\n    apply(operation) {\n      console.debug('Operation:', operation);\n      apply(operation);\n    },\n  },\n}));\n\nconst editor = createPlateEditor({\n  plugins: [LoggingPlugin],\n});\n```\n\nThis approach allows you to log operations, selections, or any other editor behavior you want to inspect.\n\n### 2. Remove Suspected Plugins\n\nIf you're experiencing issues, try removing plugins one by one to isolate the problem:\n\n```ts\nconst editor = createPlateEditor({\n  plugins: [\n    // Comment out or remove suspected plugins\n    // HeadingPlugin,\n    // BoldPlugin,\n    // ...other plugins\n  ],\n});\n```\n\nGradually add plugins back until you identify the one causing the issue.\n\n### 3. Use React DevTools\n\nReact DevTools can be invaluable for debugging Plate components:\n\n1. Install the React DevTools browser extension.\n2. Open your app and the DevTools.\n3. Navigate to the Components tab.\n4. Inspect Plate components, their props, and state.\n\n### 4. Use Browser DevTools Breakpoints\n\nSet breakpoints in your code using browser DevTools:\n\n1. Open your app in the browser and open DevTools.\n2. Navigate to the Sources tab.\n3. Find your source file and click on the line number where you want to set a breakpoint.\n4. Interact with your editor to trigger the breakpoint.\n5. Inspect variables and step through the code.\n\n### 5. Create Minimal Reproducible Examples\n\nIf you're facing a complex issue:\n\n1. Pick a [template](/docs/installation).\n2. Add only the essential plugins and components to reproduce the issue.\n3. If the issue persists, [open an issue on GitHub](https://github.com/udecode/plate/issues/new?assignees=&labels=bug&projects=&template=bug.yml) or share your example on [Discord](https://discord.gg/mAZRuBzGM3).\n\n### 6. Use Redux DevTools for zustand stores\n\nZustand and thus zustand-x works with the Redux DevTools browser extension. It can be very useful to help track state changes in zustand stores.\n\nFollow the [zustand documentation](https://zustand.docs.pmnd.rs/middlewares/devtools) to get going with Redux DevTools and zustand.\n\n\n## Debug Error Types\n\nPlate uses several predefined error types to help identify specific issues during development. Here's a list of these error types and their descriptions:\n\n### DEFAULT\n\nA general error that doesn't fit into other specific categories. Used when no other error type is applicable to the situation.\n\n### OPTION_UNDEFINED\n\nThrown when an attempt is made to access an undefined plugin option. This occurs when trying to use a plugin option that hasn't been set or is undefined.\n\n### OVERRIDE_MISSING\n\nIndicates that an expected override is missing in a plugin configuration. This happens when a plugin expects certain overrides to be provided, but they are not present in the configuration.\n\n### PLUGIN_DEPENDENCY_MISSING\n\nOccurs when a required plugin dependency is not found. This error is thrown when a plugin depends on another plugin that hasn't been registered or included in the editor configuration.\n\n### PLUGIN_MISSING\n\nIndicates an attempt to use a plugin that hasn't been registered. This happens when trying to access or use a plugin that is not part of the current editor configuration.\n\n### USE_CREATE_PLUGIN\n\nThrown when a plugin wasn't created using `createSlatePlugin` or `createPlatePlugin` function. This error occurs when a plugin is added to the editor without being properly created using the designated function.\n\n### USE_ELEMENT_CONTEXT\n\nIndicates that the `useElement` hook is being used outside of the appropriate element context. This occurs when trying to access element-specific data or functionality outside of the correct component context.\n\n### PLUGIN_NODE_TYPE\n\nThrown when a plugin is incorrectly configured as both an element and a leaf. This error occurs when a plugin's configuration contradicts itself by setting both `isElement` and `isLeaf` to true.\n",
      "type": "registry:file",
      "target": "content/docs/plate/(guides)/debugging.mdx"
    }
  ],
  "type": "registry:file"
}