Announcement

Collapse
No announcement yet.

Emitting nested arrays

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Marco
    started a topic Emitting nested arrays

    Emitting nested arrays

    I'm trying to wrap summernote, which expects a nested array of strings to initialize its toolbar, like so:
     $('#summernote').summernote({
      toolbar: [
        // [groupName,[list of button]]
        ['style', ['bold', 'italic', 'underline', 'clear']],
        ['font', ['strikethrough', 'superscript', 'subscript']],
        ['fontsize', ['fontsize']],
        ['color', ['color']],
        ['para', ['ul', 'ol', 'paragraph']],
        ['height', ['height']]  
    ] });
    See: https://summernote.org/deep-dive/#cu...oolbar-popover

    The problem is: I haven't found a clean way to create such a nested array in C#. I tried:
    • Creating an [External] class with two properties of type: string and string[]. But property names are always emitted, so I can't create a pure JS string array. Instead, the result is something like [ { Name: "Group", Buttons: [ "bold", "italic" ]} ]
    • Using Tuple<string, string[]>. This results in "item1" and "item2" properties being emitted
    Any ideas? Thanks, Marco

  • Andrey
    replied
    Thanks for update, nice idea with the enum!

    We'll discuss whether Bridge should treat external enums with [Enum(Emit.StringName*] emit option as just a string type without requirement to specify [Name] attribute explicitly. For now an error appears because Bridge expects (as usual for external entities) ToolbarButton to be defined in JavaScript, whereas it's not. So applying [Name("System.String")] attribute makes Bridge thinking it deals with just existing regular string type.

    Another way to get that issue round is to apply [Virtual] attribute instead of [External]. It works similarly except the part with type resolution. If a virtual type does not exist in JavaScript, Bridge creates a type descriptor for it dynamically.
    https://deck.net/b20771bc231b27989a7b0a06a2e352e7

    The difference between two approaches: [Name("System.String")] and [Virtual] is that a virtual type is still respected in casting. So the next sample will work for [Name] attribute and fail for [Virtual] attribute.
    object o = new[] { ToolbarButton.Bold, ToolbarButton.Italic };
    string[] strs = (string[]) o;
    Console.WriteLine(strs);

    Leave a comment:


  • Marco
    replied
    I believe I have found the solution by adding [Name("System.String")] to the enum:

    https://deck.net/b9647db93d0d85f922ea0742fc39e6e7

    Leave a comment:


  • Marco
    replied
    Hi Andrey, awesome help as always, thanks! When taking your approach a bit further, I noticed something in Bridge which could be considered a bug. Check out my sample, I just added an additional enum type:

    https://deck.net/11af946f19ebd7abafc4aad9939ebeb8

    As you will notice, this sample leads to an exception because of the missing enum type. In my wrapper I'm trying to avoid an additional bridge JS file, which is why I marked the enum as [External]. This should be valid since the enum is decorated with [Enum(Emit.StringNameLowerCase)]. In other words, bridge only exports static string constants anyway, no need for a special type.

    I suppose you should treat such enums as string / string[] in the transpiled JS and remove the type all together.

    Thanks, Marco

    Leave a comment:


  • Andrey
    replied
    Hi Marco,

    The output you posted could be achieved using Bridge.Union<string, string[]>[] type (see Union type declarations here). Probably a cleaner way would be to use Bridge.Sequence<string, string[]> originating from TypeScript Tuple type (Sequence type declarations and description).

    This is how the original sample would look like with usage of Bridge.Sequence (a type alias was used to make the sample even cleaner):

    https://deck.net/efe7cc2e12dbfbedad17d80c2eaaba84

    using Bridge;
    using ButtonsGroup = Bridge.Sequence<string, string[]>;
    
    public class Program
    {
        public static void Main()
        {
            var config = new Config
            {
                toolbar = new ButtonsGroup[] 
                {
                    new ButtonsGroup("style", new[] {"bold", "italic"}),
                    new ButtonsGroup("font", new[] {"superscript", "subscript"})
                }
            };
    
            System.Console.WriteLine(config);
        }
    }
    
    [ObjectLiteral]
    class Config
    {
        public ButtonsGroup[] toolbar;
    }
    Hope that helps.

    Leave a comment:

Working...
X