- Publicity:
Public Only
All
richtext-procs.tcl
Procedures in this file
Detailed information
[
hide source ]
| [
make this
the default ]
Content File Source
ad_library {
CKEditor 5 integration with the richtext widget of acs-templating.
In addition to the richttext widget properties,
https://openacs.org/api-doc/proc-view?proc=template::widget::richtext&source_p=1
the CKEditor 5 allows us to specify a editor class
Note: CKEditor 5 no longer comes with a configuration setting to change its height.
https://stackoverflow.com/questions/46559354/how-to-set-the-height-of-ckeditor-5-classic-editor/46559355#46559355
The current release 1.0.0-alpha.2 not for easy imaging support "Coming soon"
This script defines in essence following two procs:
::richtext-ckeditor5::initialize_widget
::richtext-ckeditor5::render_widgets
@author Gustaf Neumann
@creation-date 2 Dec 2017
@cvs-id $Id: richtext-procs.tcl,v 1.2 2018/01/19 01:23:35 gustafn Exp $
}
namespace eval ::richtext::ckeditor5 {
set package_id [apm_package_id_from_key "richtext-ckeditor5"]
set version [parameter::get \
-package_id $package_id \
-parameter CKEditorVersion \
-default 1.0.0-alpha.2]
set ckfinder_url [parameter::get \
-package_id $package_id \
-parameter CKFinderURL \
-default /acs-content-repository/ckfinder]
set standard_plugins [parameter::get \
-package_id $package_id \
-parameter StandardPlugins \
-default ""]
set JSEditorClass [parameter::get \
-package_id $package_id \
-parameter JSEditorClass \
-default ClassicEditor]
set ck_package [parameter::get \
-package_id $package_id \
-parameter CKEditorPackage \
-default "classic"]
d_proc initialize_widget {
-form_id
-text_id
{-options {}}
} {
Initialize an CKEditor richtext editor widget.
} {
ns_log notice "initialize CKEditor 5 instance with <$options>"
if {![dict exists $options extraAllowedContent]} {
dict set options extraAllowedContent {*(*)}
}
set ckOptionsList {}
if {![dict exists $options spellcheck]} {
set package_id [apm_package_id_from_key "richtext-ckeditor5"]
dict set options spellcheck [parameter::get \
-package_id $package_id \
-parameter "SCAYT" \
-default "false"]
}
lappend ckOptionsList \
"language: '[lang::conn::language]'" \
"disableNativeSpellChecker: false" \
"scayt_autoStartup: [dict get $options spellcheck]"
for {set l 0} {$l < [info level]} {incr l} {
set propVar __adp_properties(displayed_object_id)
if {[uplevel #$l [list info exists $propVar]]} {
set displayed_object_id [uplevel #$l [list set $propVar]]
break
}
}
if {[info exists displayed_object_id]} {
set image_upload_url [export_vars \
-base $::richtext::ckeditor5::ckfinder_url/uploadimage {
{object_id $displayed_object_id} {type Images}
}]
set file_upload_url [export_vars \
-base $::richtext::ckeditor5::ckfinder_url/upload {
{object_id $displayed_object_id} {type Files} {command QuickUpload}
}]
set file_browse_url [export_vars \
-base $::richtext::ckeditor5::ckfinder_url/browse {
{object_id $displayed_object_id} {type Files}
}]
lappend ckOptionsList \
"imageUploadUrl: '$image_upload_url'" \
"filebrowserBrowseUrl: '$file_browse_url'" \
"filebrowserUploadUrl: '$file_upload_url'" \
"filebrowserWindowWidth: '800'" \
"filebrowserWindowHeight: '600'"
}
set plugins [split $::richtext::ckeditor5::standard_plugins ,]
if {[dict exists $options plugins]} {
lappend plugins {*}[split [dict get $options plugins] ,]
}
if {[llength $plugins] > 0} {
lappend ckOptionsList "plugins: \[ [join $plugins ,] \]"
}
if {[dict exists $options skin]} {
lappend ckOptionsList "skin: '[dict get $options skin]'"
}
if {[dict exists $options customConfig]} {
lappend ckOptionsList \
"customConfig: '[dict get $options customConfig]'"
}
if {[dict exists $options extraAllowedContent]} {
lappend ckOptionsList \
"extraAllowedContent: '[dict get $options extraAllowedContent]'"
}
if {[dict exists $options JSEditorClass]} {
set ::richtext::ckeditor5::JSEditorClass [dict get $options JSEditorClass]
}
set JSEditorClass $::richtext::ckeditor5::JSEditorClass
set ckOptions [join $ckOptionsList ", "]
ns_log notice "initialize_widget: $JSEditorClass.create(document.querySelector( '#$text_id', {$ckOptions} )"
template::add_script -section body -script [subst {
$JSEditorClass
.create( document.querySelector( '#$text_id', {$ckOptions} ))
.catch( error => {
console.error( error );
} );
}]
::richtext::ckeditor5::add_editor
return ""
}
ad_proc render_widgets {} {} {
if {![info exists ::acs_blank_master(ckeditor5)]} {
return
}
}
d_proc ::richtext::ckeditor5::version_info {
{-ck_package ""}
{-version ""}
} {
Get information about available version(s) of CKEditor, either
from the local file system, or from CDN.
} {
if {$version eq ""} {
set version ${::richtext::ckeditor5::version}
}
if {$ck_package eq ""} {
switch ${::richtext::ckeditor5::JSEditorClass} {
ClassicEditor { set ck_package classic}
BalloonEditor { set ck_package balloon}
InlineEditor { set ck_package inline}
default { set ck_package ${::richtext::ckeditor5::ck_package}}
}
}
ns_log notice "CKeditor setting ck_package to <${::richtext::ckeditor5::ck_package}> editorclass $::richtext::ckeditor5::JSEditorClass"
set ::richtext::ckeditor5::ck_package ${::richtext::ckeditor5::ck_package}
set suffix ckeditor5/$version/$ck_package/ckeditor.js
set resources $::acs::rootdir/packages/richtext-ckeditor5/www/resources
if {[file exists $resources/$suffix]} {
lappend result file $resources/$suffix
lappend result resources /resources/richtext-ckeditor5/$suffix
}
lappend result cdn "//cdn.ckeditor.com/$suffix"
ns_log notice "CKEditor path <$result> "
return $result
}
d_proc ::richtext::ckeditor5::add_editor {
{-ck_package ""}
{-version ""}
} {
Add the necessary JavaScript and other files to the current
page. The naming is modeled after "add_script", "add_css",
... but is intended to care about everything necessary,
including the content security policies. Similar naming
conventions should be used for other editors as well.
This function can be as well used from other packages, such
e.g. from the xowiki form-fields, which provide a much higher
customization.
} {
set version_info [::richtext::ckeditor5::version_info \
-ck_package $ck_package \
-version $version]
if {[dict exists $version_info resources]} {
template::head::add_javascript \
-src [dict get $version_info resources]
} else {
template::head::add_javascript -src [dict get $version_info cdn]
security::csp::require script-src cdn.ckeditor.com
security::csp::require style-src cdn.ckeditor.com
security::csp::require img-src cdn.ckeditor.com
}
security::csp::require -force script-src 'unsafe-inline'
security::csp::require img-src data:
}
d_proc ::richtext::ckeditor5::download {
{-ck_package ""}
{-version ""}
} {
Download the CKeditor package in the specified version and put
it into a directory structure similar to the CDN structure to
allow installation of multiple versions. When the local
structure is available, it will be used by initialize_widget.
Notice, that for this automated download, the "unzip" program
must be installed and $::acs::rootdir/packages/www must be
writable by the web server.
} {
if {$version eq ""} {
set version ${::richtext::ckeditor5::version}
}
if {$ck_package eq ""} {
set ck_package ${::richtext::ckeditor5::ck_package}
}
set download_url http://download.cksource.com/CKEditor/CKEditor/CKEditor%20${version}/ckeditor_${version}_${ck_package}.zip
set resources $::acs::rootdir/packages/richtext-ckeditor5/www/resources
set unzip [::util::which unzip]
if {$unzip eq ""} {
error "can't install CKeditor locally; no unzip program found on PATH"
}
if {![file isdirectory $resources/$version]} {
file mkdir $resources/$version
}
if {![file writable $resources/$version]} {
error "directory $resources/$version is not writable"
}
set result [util::http::get -url $download_url -spool]
if {[dict get $result status] == 200} {
set fn [dict get $result file]
set output [exec $unzip -o $fn -d $resources/$version]
file rename -- \
$resources/$version/ckeditor \
$resources/$version/$ck_package
} else {
error "download of $download_url failed, HTTP status: [dict get $result status]"
}
}
ad_proc -public get_tag {-options} {} {
if {[dict exists $options editor]
&& [dict get $options editor] eq "ckeditor5"
&& [dict exists $options JSEditorClass]
&& [dict get $options JSEditorClass] ne "ClassicEditor"
} {
set edit_item_tag div
} else {
set edit_item_tag textarea
}
return $edit_item_tag
}
}