draft.js wagtail编辑器,基于draftail和draftjs_exporter
wagtaildraftail的Python项目详细描述
瓦盖德拉法蒂尔
Draft.js editor for Wagtail, built upon Draftail and draftjs_exporter.
这是alpha软件,使用风险自负。暂时不要在生产中使用。
查看Awesome Wagtail了解更多来自wagtail社区的优秀软件包和资源。
安装
从pip中获取带有pip install wagtaildraftail的包,然后将wagtaildraftail添加为django设置中的应用程序。
Note: this package contains compiled (bundled, minified) JavaScript and CSS, it can only be installed directly from pip.
使用量
There is a basic test site set up in the ^{tt3}$ folder for reference.
带页
首先,在一些页面中添加一个Draftail字段下面是一个例子:
fromwagtaildraftail.fieldsimportDraftailTextFieldclassMyPage(Page):body=DraftailTextField(blank=True)panels=[FieldPanel('body')]
然后,在显示这些字段时,使用模板中的richtext过滤器。
{% load wagtailcore_tags %} {% block content %} {{ page.body|richtext }} {% endblock %}
有流场
下面是使用现成块的示例:
fromwagtaildraftail.blocksimportDraftailTextBlockclassMyStructBlock(StructBlock):body=DraftailTextBlock()
配置
对于每个字段的自定义,DraftailTextField和DraftailTextBlock都接受字符串作为关键字参数editor。
Wagtail将在设置中查找WAGTAILADMIN_RICH_TEXT_EDITORS常量,找到请求的编辑器,加载定义的小部件并将选项(如果定义)传递给它
在WAGTAILADMIN_RICH_TEXT_EDITORS中定义的每个编辑器都是一个字典,有两个键:,WIDGET(必需)和OPTIONS(可选)。
- WIDGET是一个强制字符串,设置为要使用的小部件 -应始终设置为wagtaildraftail.widgets.DraftailTextArea(或其子类)以使用draft.js内容
- OPTIONS是遵循Draftail configuration options格式的字典。 -作为JavaScript值的Draftail选项在运行时在client/wagtaildraftail.js
警告:不应更改blockTypes、inlineStyles和entityTypes的type键它定义了内容的呈现方式,并将其保存为数据库中的json blob,这将使迁移非常痛苦。
警告:编辑器配置中定义的所有块/样式/实体都应配置为在exporter config中正确呈现
这是一个示例配置文件这应该存在于您的django设置中。
fromdraftjs_exporter.constantsimportBLOCK_TYPES,ENTITY_TYPES,INLINE_STYLESfromdraftjs_exporter.defaultsimportBLOCK_MAPTERMS_BLOCK_ID='TERMS_AND_CONDITIONS_TEXT'DRAFT_BLOCK_TYPE_H3={'label':'H3','type':BLOCK_TYPES.HEADER_THREE}DRAFT_BLOCK_TYPE_H4={'label':'H4','type':BLOCK_TYPES.HEADER_FOUR}DRAFT_BLOCK_TYPE_UL={'label':'UL','type':BLOCK_TYPES.UNORDERED_LIST_ITEM,'icon':'icon-list-ul'}DRAFT_BLOCK_TYPE_OL={'label':'OL','type':BLOCK_TYPES.ORDERED_LIST_ITEM,'icon':'icon-list-ol'}DRAFT_BLOCK_TYPE_TERMS={'label':'T&Cs','type':TERMS_BLOCK_ID,'element':'div','class':'legals'}DRAFT_INLINE_STYLE_BOLD={'label':'Bold','type':INLINE_STYLES.BOLD,'icon':'icon-bold'}DRAFT_INLINE_STYLE_ITALIC={'label':'Italic','type':INLINE_STYLES.ITALIC,'icon':'icon-italic'}# It accepts a list of dicts with `label` and `value` keys (e.g. `{'label': 'Full width', 'value': 'fullwidth'}`)# or a special `__all__` value which will be intercepted and will load all image formats known to Wagtail.DRAFT_IMAGE_FORMATS='__all__'DRAFT_ENTITY_TYPE_IMAGE={'label':'Image','type':ENTITY_TYPES.IMAGE,'icon':'icon-image','imageFormats':DRAFT_IMAGE_FORMATS,'source':'ImageSource','decorator':'Image',}DRAFT_ENTITY_TYPE_EMBED={'label':'Embed','type':ENTITY_TYPES.EMBED,'icon':'icon-media','source':'EmbedSource','decorator':'Embed',}DRAFT_ENTITY_TYPE_LINK={'label':'Link','type':ENTITY_TYPES.LINK,'icon':'icon-link','source':'LinkSource','decorator':'Link',}DRAFT_ENTITY_TYPE_DOCUMENT={'label':'Document','type':ENTITY_TYPES.DOCUMENT,'icon':'icon-doc-full','source':'DocumentSource','decorator':'Document',}WAGTAILADMIN_RICH_TEXT_EDITORS={'default_draftail':{'WIDGET':'wagtaildraftail.widgets.DraftailTextArea','OPTIONS':{'enableHorizontalRule':True,'enableLineBreak':False,'entityTypes':[DRAFT_ENTITY_TYPE_LINK,DRAFT_ENTITY_TYPE_DOCUMENT,],'blockTypes':[DRAFT_BLOCK_TYPE_H3,DRAFT_BLOCK_TYPE_UL,],'inlineStyles':[DRAFT_INLINE_STYLE_BOLD,DRAFT_INLINE_STYLE_ITALIC,],}},'format_and_link':{'WIDGET':'wagtaildraftail.widgets.DraftailTextArea','OPTIONS':{'entityTypes':[DRAFT_ENTITY_TYPE_LINK,],'inlineStyles':[DRAFT_INLINE_STYLE_BOLD,DRAFT_INLINE_STYLE_ITALIC,],}},# Wagtail dependencies'default':{'WIDGET':'wagtail.wagtailadmin.rich_text.HalloRichTextArea'},'custom':{'WIDGET':'wagtail.tests.testapp.rich_text.CustomRichTextArea'},}DRAFT_EXPORTER_ENTITY_DECORATORS={ENTITY_TYPES.LINK:'wagtaildraftail.decorators.Link',ENTITY_TYPES.DOCUMENT:'wagtaildraftail.decorators.Document',ENTITY_TYPES.IMAGE:'wagtaildraftail.decorators.Image',ENTITY_TYPES.EMBED:'wagtaildraftail.decorators.Embed',ENTITY_TYPES.HORIZONTAL_RULE:'wagtaildraftail.decorators.HR',}DRAFT_EXPORTER_COMPOSITE_DECORATORS=['wagtaildraftail.decorators.BR',]DRAFT_EXPORTER_BLOCK_MAP=dict(BLOCK_MAP,**{BLOCK_TYPES.UNORDERED_LIST_ITEM:{'element':'li','wrapper':'ul','wrapper_props':{'class':'list-styled'},},BLOCK_TYPES.ORDERED_LIST_ITEM:{'element':'li','wrapper':'ol','wrapper_props':{'class':'list-numbered'},},TERMS_BLOCK_ID:{'element':'p','props':{'class':'legals'},},})
创建新的内容格式
待办事项
创建块和内联样式
待办事项
创建实体
一个实体基本上需要4个元素:
- 一页decorator
- 编辑decorator
- 编辑source。
- 编辑strategy
decorators定义内容需要如何在站点页面上以及编辑器中显示。
- 对于页面,它们在Python中用draftjs_exporter定义请参阅the draftjs_exporter README上的专用文档
- 对于编辑器,它们是用draftail在JS/React中定义的请参阅the Draftail README上的专用文档。
源代码定义了一个接口(通常是一个模式),用户可以通过该接口选择要插入编辑器的实体
策略允许编辑器在加载时识别实体。策略是可选的,因为默认策略在大多数情况下都可以正常工作。
js/react withdraftail中定义了源和策略。请参阅the Draftail README上的专用文档。
要将装饰器、源或策略注册到wagtaildraftail,请使用相应的register函数:
window.wagtailDraftail.registerDecorators({LinkDecorator,ButtonDecorator});window.wagtailDraftail.registerSources({LinkSource});window.wagtailDraftail.registerStrategies({LinkStrategy});
注意:为了使wagtailDraftail及其注册函数在全局window命名空间中可用,请确保wagtaildraftail出现在尝试在INSTALED_APPS中注册实体的任何其他应用程序之前。
发展
安装
Requirements: ^{tt32}$, ^{tt33}$, ^{tt34}$
git clone git@github.com:springload/wagtaildraftail.git
cd wagtaildraftail/
virtualenv .venv
source ./.venv/bin/activate
make init
# Install all tested python versions
pyenv install 2.7.11 && pyenv install 3.3.6 && pyenv install 3.4.4 && pyenv install 3.5.1
pyenv global system 2.7.11 3.3.6 3.4.4 3.5.1
命令
make help# See what commands are available. make init # Install dependencies and initialise for development. make start # Starts the development server and compilation tools. make lint # Lint the project. make load-data # Prepares the database for usage. make test# Test the project. make test-coverage # Run the tests while generating test coverage data. make test-ci # Continuous integration test suite. make clean-pyc # Remove Python file artifacts. make dist # Compile the JS and CSS for release. make publish # Publishes a new version to pypi.
调试
要启动并运行,
# Set up the development environment. make init # Start the development server. make start # If necessary, start the JS compilation watch npm run start
makefile(python)和package.json(js)中都提供了测试和linting任务。
更新测试数据
以下是有用的命令:
# Create new migrations from changes to the project. python tests/manage.py makemigrations # "Reset" the database. rm db.sqlite3 # Generate fixtures from DB data. Remember to clean them up so they do not overlap with data from migrations. python tests/manage.py dumpdata > tests/fixtures/test_data.json
发布
- 为发布创建一个新分支新版本的。
- 更新CHANGELOG。
- 根据semver更新wagtaildraftail/__init__.py和package.json中的版本号
- 做一个公关和挤压合并它
- 回到合并了pr的master,使用make publish(确认并输入密码)。
- 最后,转到github并为新版本创建一个版本和一个标记。
- 完成!
文件
See the docs folder