@@ -96,6 +96,51 @@ class PlaceholderManager(
9696 insertContentOverSpanWithId(attrs.getValue(UUID_ATTRIBUTE ))
9797 }
9898
99+ /* *
100+ * Call this method to insert an item with an option to merge it with the previous item. This could be used to
101+ * build a gallery of images on adding a new image.
102+ * @param type placeholder type
103+ * @param updateItem function to update current parameters with new params
104+ */
105+ suspend fun insertOrUpdateItem (type : String , updateItem : (currentAttributes: Map <String , String >? , currentType: String? ) -> Map <String , String >) {
106+ val from = (aztecText.selectionStart - 1 ).coerceAtLeast(0 )
107+ val editableText = aztecText.editableText
108+ val currentItem = editableText.getSpans(
109+ from,
110+ aztecText.selectionStart,
111+ AztecPlaceholderSpan ::class .java
112+ ).lastOrNull()
113+ if (currentItem != null ) {
114+ val adapter = adapters[type]
115+ ? : throw IllegalArgumentException (" Adapter for inserted type not found. Register it with `registerAdapter` method" )
116+ val currentAttributes = mutableMapOf<String , String >()
117+ val uuid = currentItem.attributes.getValue(UUID_ATTRIBUTE )
118+ val currentType = currentItem.attributes.getValue(TYPE_ATTRIBUTE )
119+ for (i in 0 until currentItem.attributes.length) {
120+ val name = currentItem.attributes.getQName(i)
121+ val value = currentItem.attributes.getValue(name)
122+ currentAttributes[name] = value
123+ }
124+ val updatedAttributes = updateItem(currentAttributes, currentType)
125+ removeItem { aztecAttributes ->
126+ aztecAttributes.getValue(UUID_ATTRIBUTE ) == uuid
127+ }
128+ val attrs = AztecAttributes ().apply {
129+ updatedAttributes.forEach { (key, value) ->
130+ setValue(key, value)
131+ }
132+ }
133+ attrs.setValue(UUID_ATTRIBUTE , uuid)
134+ attrs.setValue(TYPE_ATTRIBUTE , type)
135+ val drawable = buildPlaceholderDrawable(adapter, attrs)
136+ aztecText.insertMediaSpan(AztecPlaceholderSpan (aztecText.context, drawable, 0 , attrs,
137+ this , aztecText, WeakReference (adapter), TAG = htmlTag))
138+ insertContentOverSpanWithId(uuid)
139+ } else {
140+ insertItem(type, * updateItem(null , null ).toList().toTypedArray())
141+ }
142+ }
143+
99144 /* *
100145 * Call this method to remove a placeholder from both the AztecText and the overlaying layer programatically.
101146 * @param predicate determines whether a span should be removed
0 commit comments