This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TinyMCE_on_Pyqt.py
85 lines (69 loc) · 3.76 KB
/
TinyMCE_on_Pyqt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python3
import sys,os
from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from PySide6.QtWebEngineWidgets import *
from PySide6.QtWebChannel import *
import logging
import json
import threading
logging.basicConfig(level=logging.DEBUG) #设置日志等级
DIR = os.path.dirname(__file__)
class TinyMCE_on_PyQt_window(QWidget):
def __init__(self):
super(TinyMCE_on_PyQt_window, self).__init__()
self.url = QFileInfo(DIR + "/main.html").absoluteFilePath()
self.layout = QVBoxLayout()
self.TinyMCE_config = json.dumps({ #转为json方便与JS交互
'selector': '#TinyMCE',
'language': 'zh_CN',
'height': 'calc(100vh)',
'plugins':'code,table,autosave,lists,advlist,anchor,autolink,charmap,emoticons,insertdatetime,media,preview,quickbars,searchreplace,template,wordcount',
'toolbar': ['aligncenter | alignjustify | alignleft | alignnone | alignright | blockquote | backcolor | blocks | bold | copy | cut | fontfamily | fontsize | forecolor | h1 | h2 | h3 | h4 | h5 | h6 | newdocument | outdent | paste | pastetext | print | redo | remove | removeformat | selectall | strikethrough | styles | subscript | superscript | underline | undo | visualaid | code | restoredraft | bullist | anchor | link | charmap | emoticons | insertdatetime | media | preview | searchreplace | table tabledelete | tableprops tablerowprops tablecellprops | tableinsertrowbefore tableinsertrowafter tabledeleterow | tableinsertcolbefore tableinsertcolafter tabledeletecol | template | wordcount']
})
self.refresh_state = True #TinyMCE文本刷新状态,True代表刷新完成内容可用,False代表正在等待JS回调
self.init_finished = False
def init(self):
# 创建浏览器控件
self.browser = QWebEngineView()
if sys.platform == "win32" or sys.platform == "win64":
self.browser.load(QUrl(self.url)) # Windows下特殊情况
else:
self.browser.load(QUrl("file://"+self.url))
# 新建QWebChannel
self.channel = QWebChannel()
self.browser.page().setWebChannel(self.channel)
self.browser.page().loadFinished.connect(self.init_TinyMCE)
# 设置QWidget的layout
self.layout.addWidget(self.browser)
self.setLayout(self.layout)
def init_TinyMCE(self):
#初始化TinyMCE(调用JS)
self.browser.page().runJavaScript("init_editor('%s');" % self.TinyMCE_config)
self.init_finished = True
# 刷新TinyMCE编辑器的内容
def __call_back(self,result):
logging.info("已收到call back")
self.content=result
self.refresh_state=True #回调完成,self.content内容已刷新,内容 可用(最新)
def refresh_content(self):
logging.info("开始刷新TinyMCE内容")
self.refresh_state=False #开始等待回调,内容 不可用(不一定最新)
self.browser.page().runJavaScript("tinymce.activeEditor.getContent();",0,self.__call_back)
# 当newest为True时,进程将堵塞等待刷新完成
def get_file(self):
self.refresh_content() #开始刷新内容
logging.info("开始堵塞,等待刷新完成")
while not self.refresh_state: # 当内容没有刷新完成时进行堵塞
QApplication.processEvents()
pass
return self.content
def save_file(self,url,refresh=True):
with open(url, mode='w', encoding='utf-8') as file_obj:
if refresh:
file_obj.write(self.get_file())
else:
file_obj.write(self.content)
def set_html(self,html_code):
self.browser.page().runJavaScript("tinymce.activeEditor.setContent('%s');" % html_code)