You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:在Angular 4项目中集成Tinymce与ResponsiveFileManager的步骤

搞定Angular 4 + TinyMCE + ResponsiveFileManager 配置全流程

我刚帮几个Angular老项目搞定过这个组合配置,踩了不少坑,给你一步步拆解,保证能顺利跑起来:

1. 先把依赖和资源准备好

  • 用npm装适配Angular4的TinyMCE封装:npm install @tinymce/tinymce-angular@2.5.0 --save(划重点:Angular4别装太新的版本,2.x是完美适配的)
  • 下载ResponsiveFileManager的最新压缩包,解压后把整个文件夹丢到项目的src/assets目录里,改名叫filemanager,方便后续引用。

2. 在Angular模块里注册编辑器模块

打开你要用到编辑器的模块(比如AppModule),把TinyMCE的模块导进去:

import { EditorModule } from '@tinymce/tinymce-angular';

@NgModule({
  imports: [
    // 你的其他基础模块(比如BrowserModule)
    EditorModule
  ]
})
export class AppModule { }

3. 给TinyMCE加FileManager的回调配置

在使用编辑器的组件里,写好初始化配置,核心是加上file_picker_callback来对接ResponsiveFileManager:

import { Component } from '@angular/core';

@Component({
  selector: 'app-my-editor',
  template: `<editor [init]="editorConfig"></editor>`
})
export class MyEditorComponent {
  editorConfig = {
    height: 500,
    plugins: 'image link media code', // 至少要包含image/link这类需要文件选择的插件
    toolbar: 'undo redo | bold italic | image link media | code', // 对应工具栏按钮
    // 关键:对接FileManager的核心回调
    file_picker_callback: (callback, value, meta) => {
      // 根据文件类型打开对应的FileManager弹窗
      const fileType = meta.filetype === 'image' ? 'image' : 'file';
      const filemanagerUrl = `/assets/filemanager/dialog.php?type=${fileType}`;
      // 打开FileManager弹窗
      const win = window.open(filemanagerUrl, 'filemanager', 'width=900,height=600');
      // 监听FileManager的返回事件,把选中的路径传给TinyMCE
      window['setFile'] = function (selectedUrl) {
        win.close();
        callback(selectedUrl);
      };
    },
    // 可选:如果要直接上传图片,用FileManager自带的上传接口即可
    image_upload_url: '/assets/filemanager/upload.php',
    content_style: 'body { font-family: Arial, sans-serif; font-size: 14px; }'
  };
}

4. 配置ResponsiveFileManager的核心参数

打开src/assets/filemanager/config/config.php,修改这几个关键项:

  • $base_url:改成你的项目根地址,本地开发就是http://localhost:4200
  • $upload_dir:文件上传的目录,填/assets/filemanager/userfiles/
  • $current_path:对应相对路径,填../userfiles/
  • 检查$allow_extensions,确保包含你需要的文件类型(比如jpg、png、pdf等)

5. 确保Angular能访问FileManager的静态文件

Angular4用的是.angular-cli.json(新版本是angular.json),打开它在assets数组里加上FileManager的配置,确保打包时能把这些文件带进去:

"assets": [
  "assets",
  "favicon.ico",
  {
    "glob": "**/*",
    "input": "./src/assets/filemanager",
    "output": "./assets/filemanager"
  }
]

6. 测试运行

启动项目:ng serve,打开编辑器页面,点击图片或者链接按钮,应该能弹出ResponsiveFileManager的窗口,选择或上传文件后,路径会自动插入到TinyMCE里。

几个踩过的坑提醒你

  • 跨域问题:本地开发时别用不同端口,确保主页面和FileManager弹窗同域
  • 权限问题:给assets/filemanager/userfiles目录开读写权限,不然上传会失败
  • TinyMCE版本:一定要装适配Angular4的2.x版本,装新了会出现兼容性报错

内容的提问来源于stack exchange,提问作者tfa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:46:50