<?php
/*
Plugin Name: TechCode PrismJS
Plugin URI: https://wujie.me/techcode-prismjs
Description: 为WordPress添加Prism.js代码高亮支持
Version: 1.0
Author: Your Name
Author URI: https://wujie.me
License: GPL v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/

if (!defined('ABSPATH')) exit;

class TechCode_PrismJS {
    public function __construct() {
        add_action('wp_enqueue_scripts', array($this, 'enqueue_assets'));
        add_action('enqueue_block_editor_assets', array($this, 'enqueue_editor_assets'));
        add_action('init', array($this, 'register_block_attributes'));
    }

    // 为编辑器加载样式
    public function enqueue_editor_assets() {
        // 启用编辑器样式支持
        add_theme_support('editor-styles');
        
        // 加载 Prism.js 样式
        wp_enqueue_style(
            'techcode-prism-editor',
            plugins_url('assets/css/prism.css', __FILE__)
        );

        // 加载编辑器脚本
        wp_enqueue_script(
            'techcode-prism-editor',
            plugins_url('assets/js/editor.js', __FILE__),
            array('wp-blocks', 'wp-element', 'wp-editor')
        );

        // 编辑器特定的样式
        $editor_css = "
            /* 代码块基础样式 */
            .wp-block-code {
                font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
                font-size: 14px !important;
            }
            
            /* 行内代码样式 */
            .editor-styles-wrapper :not(pre) > code {
                font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
                font-size: 0.875em;
                background: #fff5f5;
                color: #e34c26;
                padding: 1px 6px;
                border: 1px solid #ffe3e3;
                border-radius: 4px;
                margin: 0 2px;
                word-wrap: break-word;
                max-width: 100%;
                box-decoration-break: clone;
                -webkit-box-decoration-break: clone;
            }
        ";
        wp_add_inline_style('techcode-prism-editor', $editor_css);
    }

    public function enqueue_assets() {
        // 加载 Prism 核心样式
        wp_enqueue_style(
            'techcode-prism',
            plugins_url('assets/css/prism.css', __FILE__)
        );
        
        // 按顺序加载 Prism 核心和插件脚本
        wp_enqueue_script(
            'techcode-prism',
            plugins_url('assets/js/prism.js', __FILE__),
            array(),
            '1.0',
            true
        );

        // 新版本，插件已经合并到核心脚本中
        // wp_enqueue_script(
        //     'techcode-prism-toolbar',
        //     plugins_url('assets/js/prism-toolbar.min.js', __FILE__),
        //     array('techcode-prism'),
        //     '1.0',
        //     true
        // );

        // wp_enqueue_script(
        //     'techcode-prism-copy',
        //     plugins_url('assets/js/prism-copy-to-clipboard.min.js', __FILE__),
        //     array('techcode-prism-toolbar'),
        //     '1.0',
        //     true
        // );

        // 自定义样式
        $custom_css = "
            /* 块级代码样式 */
            code[class*='language-'],
            pre[class*='language-'] {
                font-size: 14px;
            }
            
            /* Show Language 插件样式优化 */
            /*
            div.code-toolbar > .toolbar {
                right: 0.5em;
            }

            div.code-toolbar > .toolbar > .toolbar-item > span {
                background: #f5f5f5;
                border: 1px solid #ddd;
                border-radius: 3px;
                color: #666;
                font-size: 0.8em;
                padding: 2px 6px;
            }
                */

            /* 行内代码样式 */
            :not(pre) > code {
                font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
                font-size: 0.875em;
                background: #fff5f5;
                color: #e34c26;
                padding: 1px 6px;
                border: 1px solid #ffe3e3;
                border-radius: 4px;
                margin: 0 2px;
                word-wrap: break-word;
                max-width: 100%;
                box-decoration-break: clone;
                -webkit-box-decoration-break: clone;
            }
            
            /* 工具栏样式优化 */
            /*
            div.code-toolbar > .toolbar {
                opacity: 0;
                transition: opacity 0.3s;
            }
            
            div.code-toolbar:hover > .toolbar {
                opacity: 1;
            }
            
            div.code-toolbar > .toolbar > .toolbar-item > button {
                padding: 4px 8px;
                font-size: 12px;
                color: #666;
                background: #fff;
                border: 1px solid #ddd;
                border-radius: 3px;
                cursor: pointer;
            }
            
            div.code-toolbar > .toolbar > .toolbar-item > button:hover {
                background: #f5f5f5;
                color: #333;
            }
                */
        ";
        wp_add_inline_style('techcode-prism', $custom_css);

        wp_add_inline_script('techcode-prism', '
        document.addEventListener("DOMContentLoaded", function() {
            // 启用 Show Language 插件
            Prism.plugins.toolbar.registerButton("show-language", function(env) {
                return {
                    text: env.language
                };
            });

            document.querySelectorAll("pre.wp-block-code").forEach(function(block) {
                let content = block.textContent;
                let language = "plaintext";
                
                // 1. 检查 Gutenberg 的 language 属性
                let blockLanguage = block.getAttribute("data-language");
                if (blockLanguage) {
                    language = blockLanguage;
                }
                // 2. 检查 class 中的语言标记
                else {
                    let existingLang = block.className.match(/language-([a-z]+)/i);
                    if (existingLang) {
                        language = existingLang[1].toLowerCase();
                    }
                    // 3. 检查注释方式的语言标记
                    else {
                        let langMatch = content.match(/^\/\/\s*language:\s*([a-z]+)/i);
                        if (langMatch) {
                            language = langMatch[1].toLowerCase();
                            content = content.replace(/^\/\/\s*language:\s*[a-z]+\s*\n/i, "");
                        }
                    }
                }
                
                block.className = "wp-block-code language-" + language;
                
                let codeElement = block.querySelector("code");
                if (!codeElement) {
                    codeElement = document.createElement("code");
                    codeElement.className = "language-" + language;
                    codeElement.textContent = content;
                    block.textContent = "";
                    block.appendChild(codeElement);
                } else {
                    codeElement.className = "language-" + language;
                    codeElement.textContent = content;
                }
            });
            
            if (typeof Prism !== "undefined") {
                Prism.highlightAll();
            }
        });
    ');
    }

    // 添加这个新方法
    public function register_block_attributes() {
        register_block_type('core/code', array(
            'attributes' => array(
                'language' => array(
                    'type' => 'string',
                    'default' => ''
                ),
                'className' => array(
                    'type' => 'string'
                )
            ),
            'supports' => array(
                'className' => true,
                'customClassName' => true
            )
        ));
    }
}

new TechCode_PrismJS();
