
    h1                        d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZmZ ddlmZ ddlmZmZm Z  ddl!m"Z"m#Z# ddl!m$Z% ed        Z& G d dejN                        Z(y)    N)deepcopy)cache)groupby)Union)forms)settings)DjangoJSONEncoder)models)render_to_string)NoReverseMatch)	mark_safe)get_languagegettext   )DEFAULT_TOOLBAR_CMSDEFAULT_TOOLBAR_HTMLFieldget_editor_config)admin_reversecms_placeholder_add_plugin)__version__c                      ddl m}  | j                  j                         D ]%  }t	        |d      st        |j                        c S  	 t        d      S # t        $ r Y yw xY w)z;Get the url for dynamic liks for cms plugins and HTMLFieldsr   )siteglobal_link_url_name,djangocms_text_textplugin_get_available_urlsN)django.contrib.adminr   	_registryvalueshasattrr   r   r   )r   model_admins     F/home/dcms/DCMS/lib/python3.12/site-packages/djangocms_text/widgets.pyget_url_endpointr!      sd     *~~,,. C; 67 !A!ABBCKLL s   

A 	A! A!c                        e Zd ZdZed        Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddeeef   deee	f   dede	deded	ed
edede
dede
f fdZd fd	Zd Zd Zd ZddZddZ xZS )TextEditorWidgeta  
    A widget for editing text content and plugins in a CMS environment.

    This class extends the standard Django forms.Textarea widget, providing additional
    capabilities to edit and manage content enriched with plugins. It integrates with
    CMS systems and supports rendering, configuration, and customization of the text editor.
    The widget is designed to adapt to a plugin-based architecture for seamless content creation
    and management. It leverages specific editor settings, installed plugins, and placeholders,
    while also supporting dynamic configurations tailored to individual plugin instances.

    Attributes:
        editor_class (str): The CSS class used to initialize the text editor.
        editor_settings_id (str): Unique identifier for widget-specific editor settings.
        global_settings_id (str): Shared identifier for global editor settings.
        installed_plugins (list): A list of plugins available for text enhancement.
        pk (str | int): The primary key of the associated plugin instance.
        placeholder (str | int | None): The placeholder associated with the widget, if applicable.
        plugin_language (str | None): The language used within the plugin.
        plugin_position (int | None): Position of the plugin relative to others.
        configuration (dict): Configuration settings for the text editor.
        cancel_url (str | None): URL used to cancel editor actions.
        url_endpoint (str | None): Endpoint for editor-related HTTP API calls.
        render_plugin_url (str | None): URL for rendering plugin content.
        messages_url (str | None): URL for fetching editor-related messages.
        action_token (str | None): A token used to perform secured editor actions.
        revert_on_cancel (bool): Whether changes are reverted upon cancellation.
        body_css_classes (str): CSS classes for text editor's body element.

    Args:
        attrs: Optional dictionary of widget attributes.
        installed_plugins: A list containing details of plugins enabled for the widget.
        pk: The primary key identifying the plugin instance.
        placeholder: A placeholder instance or its identifier for the widget's context.
        plugin_language: A string specifying the language of the plugin in the editor context.
        plugin_position: The integer position of the plugin among others.
        configuration: Optional custom configuration dictionary for the widget editor.
        cancel_url: A string representing the URL to redirect after cancellation.
        url_endpoint: A string URL endpoint for backend interaction.
        render_plugin_url: A string URL for rendering plugin output.
        messages_url: A string URL for retrieving informational messages.
        action_token: A secure action token string for backend interaction.
        revert_on_cancel: A boolean flag to enable or disable reversion of changes on cancellation.
        body_css_classes: A string for CSS classes to be attached to the editor body.
    c                 4   | j                   j                  j                  dd      }| j                  r|| j                   j                  z  }t        j                  i | j                   j                  dddg|idg| j                   j                        S )Nall zdjangocms_text/css/cms.text.cssz$djangocms_text/css/cms.normalize.cssz+djangocms_text/bundles/bundle.editor.min.js)cssjs)
rte_configr'   getadd_admin_css	admin_cssr   Mediar(   )selfrte_csss     r    mediazTextEditorWidget.mediaS   s    //%%))%4t000G{{//%%5:  >##
 	
    attrspkplugin_languageplugin_position
cancel_urlurl_endpointrender_plugin_urlmessages_urlaction_tokenrevert_on_cancelbody_css_classesr+   c                    |i }t               | _        d| _        | j                  |j                  dd      j	                  d      vr4|j                  dd       d| j                   }|j                         |d<   d|r|n#|j                  dt        j                                | _        d| _	        | j                  |d	<   t        | -  |       |xs g | _        || _        t        |t        j                         r|j                  n|| _        || _        || _        |rt)        t*        |d
      rwt-        | j                  j.                        | _        | j.                  j1                  t*        j2                         | j.                  j1                  t)        t*        |             nMt-        | j                  j.                        | _        | j.                  j1                  t4        j2                         || _        |	| _        |
| _        || _        || _        || _         |r|n| j.                  j                  dd      | _!        || _"        y)z
        Create a widget for editing text + plugins.

        installed_plugins is a list of plugins to display that are text_enabled
        N
CMS_Editorclass  zcms-cfg-idzcms-editor-cfgzdata-settingsF	bodyClass)#r   r)   editor_classr*   joinstripuuiduuid4editor_settings_idglobal_settings_idsuper__init__installed_pluginsr3   
isinstancer
   Modelplaceholderr4   r5   getattrr   r   configurationupdateTEXT_EDITOR_SETTINGStext_settingsr6   r7   r8   r9   r:   r;   r<   r+   )r.   r2   rM   r3   rP   r4   r5   rR   r6   r7   r8   r9   r:   r;   r<   r+   	new_class	__class__s                    r    rL   zTextEditorWidget.__init__g   s   . =E+-(EIIgr$:$?$?$DD 99Wb12!D4E4E3FGI&__.E'N$,2R599T4::<;X,Y"Z"2!%!8!8o!2!8b-7V\\-R;>>Xc..WX}eD!)$//*G*G!HD%%h&C&CD%%gh&FG!)$//*G*G!HD%%m&H&HI$(!2(( 04D 0$J\J\J`J`alnpJq*r1   c                 (    t         |   ||||      S N)rK   render)r.   namevaluer2   rendererrW   s        r    render_textareaz TextEditorWidget.render_textarea   s    w~dE5(;;r1   c                 L   t        | j                        }| j                  r|j                  dd      }n*|j                  dd      dk(  rd}n|j                  dd      }d| |v r|d|    |d<   n|dk(  rt        nt
        |d<   t        |j                               D ]  }|j                  d      s||=  | j                  |d<   t        j                  |t              }| j                         | j                  | j                  | j                   | j"                  | j                  r| j                  nd| j$                  xs d| j&                  xs d	t        j(                  |j+                  d
|            d	j-                         D ci c]
  \  }}|r|| c}}S c c}}w )zZThe editor settings are specific for the widget and change by plugin instance or HTMLFieldtoolbarCMSF	HTMLFieldtoolbar_rC   )clsNr@   z{{ language }})	pluginsrM   	plugin_idr4   r5   placeholder_idr;   r:   options)r   rR   rP   r*   r   r   listkeys
startswithr<   jsondumpsr	   get_installed_pluginsrM   r3   r4   r5   r;   r:   loadsreplaceitems)r.   languagerR   r`   keyconfigr\   s          r    get_editor_settingsz$TextEditorWidget.get_editor_settings   s    !3!34#''	59G y%0E9!G $''	;?GgY=0'4xy5I'JM)$>E>N':TmM)$ **,- 	'C~~j)!#&	' &*%:%:k"M/@A
  557%)%;%;!WW#'#7#7#'#7#76:6F6F$"2"2D$($9$9$BU $ 1 1 7R::fnn5Ex&PQ
 eg
U  J
 	
 
s   F c                    | j                   rmt        t        | j                   d       d       }|D cg c]9  \  }}||D cg c]%  }|j                  d      |j                  d      d' c}d; c}}}S g S c c}w c c}}}w )zGroups plugins by modulec                 $    | j                  d      S Nmoduler*   xs    r    <lambda>z8TextEditorWidget.get_installed_plugins.<locals>.<lambda>   s    QUU8_ r1   )rs   c                 $    | j                  d      S rx   rz   r{   s    r    r}   z8TextEditorWidget.get_installed_plugins.<locals>.<lambda>   s    aeeHo r1   r[   r\   )titletype)grouprq   )rM   r   sortedr*   )r.   re   r   rq   items        r    rn   z&TextEditorWidget.get_installed_plugins   s    !!t--3LM-G %, 
 !E5 #afgY](8$((7BSTg  I hs   B*A<.B<Bc           
         | j                   j                         }| j                  D ]  }|d   |d   d||d   <    t        dk7  rt	        t
              nd| j                  xs
 t               t        j                  rt        j                  dz   nd|t        d      t        d	      t        d
      t        d      d|| j                  xs d| j                  xs d| j                  xs dd	| j                   j                  S )z{The global settings are shared by all widgets and are the same for all instances. They only need
        to be loaded once.r[   icon)r   r   r\   0r@   djangocms_textzCMS PluginszAdd CMS PluginzEdit CMS Plugin)r`   addeditaria)	add_plugin_urlr7   
static_urllanglang_altrr   r8   cancel_plugin_urlr9   )r)   get_base_configrM   cms_versionr   r   r7   r!   r   
STATIC_URLr   r8   r6   r9   additional_context)r.   rr   toolbar_settingplugins       r    get_global_settingsz$TextEditorWidget.get_global_settings   s    //99;,, 	Fv0OF7O,	 MX[^L^}-GHdf --C1A1CDLDWDW(--0@@]_#"=1/0 12.	 !!%!7!7!=2!%!6B --3
 oo00
 	
r1   c                    |d   }t               j                  d      d   }i d| j                  d|d|j                  dd      d|d	|d
t        j
                  d| j                  d| j                  d| j                  d| j                  d| j                  d| d|d| j                  |      d| j                  d| j                  |      d| j                  }t        t!        d|            S )NrB   -r   rD   editor_selectoreditor_function_r[   rr   r   rM   	plugin_pkr4   r5   rP   widgetr]   editor_settingsrI   global_settingsrJ   zcms/plugins/widgets/editor.html)r   splitrD   rp   r   r   rM   r3   r4   r5   rP   ru   rI   r   rJ   r   r   )r.   r[   r\   r2   r]   r   rr   contexts           r    render_additionsz!TextEditorWidget.render_additions   s^   +>'',Q/
D--

 66sC@
 D	

 
 (--
  !7!7
 
 t33
 t33
 4++
 d
 
 t77A
 !$"9"9
  t77A!
" !$"9"9#
& )*KWUVVr1   c                 R    | j                  |||      | j                  ||||      z   S rY   )r^   r   )r.   r[   r\   r2   r]   s        r    rZ   zTextEditorWidget.render  s1    ##D%7$:O:OPTV[]bdl:mmmr1   )NNNNNNNNNNNNFr@   F)NN)__name__
__module____qualname____doc__propertyr0   dictstrr   intboolrL   r^   ru   rn   r   r   rZ   __classcell__)rW   s   @r    r#   r#   %   s    +Z 
 
* !%"## !%  !& "#!7+CH~7+ #s(O	7+ 7+ 7+ 7+ 7+ 7+ 7+ 7+ 7+ 7+  !7+r<)
V"
:W4nr1   r#   ))rl   rG   copyr   	functoolsr   	itertoolsr   typingr   djangor   django.confr   django.core.serializers.jsonr	   	django.dbr
   django.template.loaderr   django.urls.exceptionsr   django.utils.safestringr   #django.utils.translation.trans_realr   r   r@   rU   editorsr   r   r   utilsr   r   r   r   r!   Textarear#   r&   r1   r    <module>r      sf             :  3 1 - E ' V V < - 
 
rnu~~ rnr1   